php Wordpress函数add_meta_box()示例
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php Wordpress函数add_meta_box()示例相关的知识,希望对你有一定的参考价值。
<?php echo $description = get_post_meta($post->ID, "description_value", true); ?>
<?php echo $keywords = get_post_meta($post->ID, "keywords_value", true); ?>
<?php
$new_meta_boxes =
array(
"description" => array(
"name" => "description",
"std" => "",
"title" => "description:"),
"keywords" => array(
"name" => "keywords",
"std" => "",
"title" => "keywords:")
);
function new_meta_boxes() {
global $post, $new_meta_boxes;
foreach($new_meta_boxes as $meta_box) {
$meta_box_value = get_post_meta($post->ID, $meta_box['name'].'_value', true);
if($meta_box_value == "")
$meta_box_value = $meta_box['std'];
echo'<input type="hidden" name="'.$meta_box['name'].'_noncename" id="'.$meta_box['name'].'_noncename" value="'.wp_create_nonce( plugin_basename(__FILE__) ).'" />';
echo'<h4>'.$meta_box['title'].'</h4>';
echo '<textarea cols="60" rows="3" name="'.$meta_box['name'].'_value">'.$meta_box_value.'</textarea><br />';
}
}
function create_meta_box() {
global $theme_name;
if ( function_exists('add_meta_box') ) {
add_meta_box( 'new-meta-boxes', 'new meta box', 'new_meta_boxes', 'post', 'normal', 'high' );
}
}
function save_postdata( $post_id ) {
global $post, $new_meta_boxes;
foreach($new_meta_boxes as $meta_box) {
if ( !wp_verify_nonce( $_POST[$meta_box['name'].'_noncename'], plugin_basename(__FILE__) )) {
return $post_id;
}
if ( 'page' == $_POST['post_type'] ) {
if ( !current_user_can( 'edit_page', $post_id ))
return $post_id;
}
else {
if ( !current_user_can( 'edit_post', $post_id ))
return $post_id;
}
$data = $_POST[$meta_box['name'].'_value'];
if(get_post_meta($post_id, $meta_box['name'].'_value') == "")
add_post_meta($post_id, $meta_box['name'].'_value', $data, true);
elseif($data != get_post_meta($post_id, $meta_box['name'].'_value', true))
update_post_meta($post_id, $meta_box['name'].'_value', $data);
elseif($data == "")
delete_post_meta($post_id, $meta_box['name'].'_value', get_post_meta($post_id, $meta_box['name'].'_value', true));
}
}
add_action('admin_menu', 'create_meta_box');
add_action('save_post', 'save_postdata');
?>
以上是关于php Wordpress函数add_meta_box()示例的主要内容,如果未能解决你的问题,请参考以下文章
php WordPress PHP函数 - 树枝的图像数组
如何在 AngularJS 部分文件中使用 php (wordpress) 函数?
板邓:wordpress标签调用函数大全
php PHP函数预处理WordPress卡的twig模板
如何在自定义 PHP 脚本中调用 WordPress 函数
如何在自定义 .php 文件中包含 WordPress 函数?