|
类似添加文章时 侧边显示分类目录、标签之类信息的地方
- 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, '_my_meta_value_key', $mydata, true) or
- update_post_meta($post_ID, '_my_meta_value_key', $mydata);
- // 或自定义表(见下面的进一步阅读的部分)
- }
- ?>
复制代码
- /**
- * 在文章编辑界面调用这个类
- */
- 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); // 输出自定义字段的值
- }
复制代码 |
|