微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

WordPress添加Meta模块函数:add_meta_box

描述

add_Meta_Box() 函数是在 wordpress 2.5 添加的,用来给插件开发添加 Meta模块 到管理界面。

用法

<?PHP
add_Meta_Box( $id,$title,$callback,$post_type,$context,$priority,$callback_args );
?>

参数

$id

(字符串)(必需)Meta模块的 HTML“ID”属性

$title

(字符串)(必需)Meta模块的标题,对用户可见

$callback

(回调)(必需)为Meta模块输出 HTML代码函数

$post_type

(字符串)(必需)显示Meta模块的文章类型,可以是文章(post)、页面(page)、链接(link)、附件(attachment) 或 自定义文章类型自定义文章类型的别名)

$context

(字符串)(可选)Meta模块的显示位置(’normal’,’advanced’,或 ‘side’)

认值:’advanced’

$priority

(字符串)(可选)Meta模块显示的优先级别(’high’,‘core’,‘default’or ‘low’)

认值: ‘default’

$callback_args

(数组)(可选)传递到 callback 函数的参数。callback 函数将接收 $post 对象和其他由这个变量传递的任何参数。

示例

下面是一个例子,在文章页面编辑界面上添加自定义栏目:

<?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,$mydata,true) or
update_post_Meta($post_ID,$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 ),'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); // 输出自定义字段的值
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐