add_meta_box示例2
下面是一个例子,在文章和页面编辑界面上添加自定义栏目:
<?php
/* 定义自定义Meta模块 */
add_action( 'add_meta_boxes', 'myplugin_add_custom_box' );
// 向后兼容(WP3.0前)
// add_action( 'admin_init', 'myplugin_add_custom_box', 1 );
/* 写入数据*/
add_action( 'save_post', 'myplugin_save_postdata' );
/*在文章和页面编辑界面的主栏中添加一个模块 */
function myplugin_add_custom_box() {
$screens = array( 'post', 'page' );
foreach ($screens as $screen) {
add_meta_box(
'myplugin_sectionid',
__( 'My Post Section Title', 'myplugin_textdomain' ),
'myplugin_inner_custom_box',
$screen
);
}
}
/* 输出模块内容 */
function myplugin_inner_custom_box( $post ) {
// 使用随机数进行核查
wp_nonce_field( plugin_basename( __FILE__ ), 'myplugin_noncename' );
// 用于数据输入的实际字段
// 使用 get_post_meta 从数据库中检索现有的值,并应用到表单中
$value = get_post_meta( $post->ID, '_my_meta_value_key', true );
echo '<label for="myplugin_new_field">';
_e("Description for this field", 'myplugin_textdomain' );
echo '</label> ';
echo '<input type="text" id="myplugin_new_field" name="myplugin_new_field" value="'.esc_attr($value).'" size="25" />';
}
/* 文章保存时,保存我们的自定义数据*/
function myplugin_save_postdata( $post_id ) {
// 首先,我们需要检查当前用户是否被授权做这个动作。
if ( 'page' == $_POST['post_type'] ) {
if ( ! current_user_can( 'edit_page', $post_id ) )
return;
} else {
if ( ! current_user_can( 'edit_post', $post_id ) )
return;
}
// 其次,我们需要检查,是否用户想改变这个值。
if ( ! isset( $_POST['myplugin_noncename'] ) || ! wp_verify_nonce( $_POST['myplugin_noncename'], plugin_basename( __FILE__ ) ) )
return;
// 第三,我们可以保存值到数据库中
//如果保存在自定义的表,获取文章ID
$post_ID = $_POST['post_ID'];
//过滤用户输入
$mydata = sanitize_text_field( $_POST['myplugin_new_field'] );
// 使用$mydata做些什么
// 或者使用
add_post_meta($post_ID, '_my_meta_value_key', $mydata, true) or
update_post_meta($post_ID, '_my_meta_value_key', $mydata);
// 或自定义表(见下面的进一步阅读的部分)
}
?>
这是一个例子,如何从一个类内部添加Meta模块
/**
* 在文章编辑界面调用这个类
*/
function call_someClass()
{
return new someClass();
}
if ( is_admin() )
add_action( 'load-post.php', 'call_someClass' );
/**
* 这个类
*/
class someClass
{
const LANG = 'some_textdomain';
public function __construct()
{
add_action( 'add_meta_boxes', array( &$this, 'add_some_meta_box' ) );
}
/**
* 添加Meta模块
*/
public function add_some_meta_box()
{
add_meta_box(
'some_meta_box_name'
,__( 'Some Meta Box Headline', self::LANG )
,array( &$this, 'render_meta_box_content' )
,'post'
,'advanced'
,'high'
);
}
/**
* 呈送Meta模块内容
*/
public function render_meta_box_content()
{
echo '<h1>TEST OUTPUT - this gets rendered inside the meta box.</h1>';
}
}
回调数组
$callback_args 数组将被传递给回调函数的第二个参数。第一个参数是这篇文章的 $post 对象。
// 这个函数添加一个带有回调函数 my_metabox_callback() 的Meta模块
function add_my_meta_box() {
$var1 = 'this';
$var2 = 'that';
add_meta_box(
'metabox_id',
'Metabox Title',
'my_metabox_callback',
'page',
'normal',
'low',
array( 'foo' => $var1, 'bar' => $var2)
);
}
// $post 是一个包含当前文章的对象 (作为一个 $post 对象)
// $metabox 是一个数组,包含模块 id, title, callback, and args elements.
// args element 是一个包含传递到 $callback_args 变量的数组
function my_metabox_callback ( $post, $metabox ) {
echo 'Last Modified: '.$post->post_modified; // 输出文章最后编辑的时间
echo $metabox['args']['foo']; // 输出 'this'
echo $metabox['args']['bar']; // 输出 'that'
echo get_post_meta($post->ID,'my_custom_field',true); // 输出自定义字段的值
}
总结
将这些分解动作整合你就可以做出一个漂亮、实用的设置框了。好了,去折腾你的后台吧。
1 2

