5ucms论坛

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 13711|回复: 2
打印 上一主题 下一主题

[插件/开发] wordpress插件开发教程05 添加后台元数据框 add_meta_box

[复制链接]

670

主题

785

帖子

8273

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
8273
跳转到指定楼层
楼主
发表于 2021-6-10 22:31:42 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
类似添加文章时 侧边显示分类目录、标签之类信息的地方

  1. 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 对象和其他由这个变量传递的任何参数。

  1. <?php
  2. /* 定义自定义Meta模块 */

  3. add_action( 'add_meta_boxes', 'myplugin_add_custom_box' );

  4. // 向后兼容(WP3.0前)
  5. // add_action( 'admin_init', 'myplugin_add_custom_box', 1 );

  6. /* 写入数据*/
  7. add_action( 'save_post', 'myplugin_save_postdata' );

  8. /*在文章和页面编辑界面的主栏中添加一个模块 */
  9. function myplugin_add_custom_box() {
  10.     $screens = array( 'post', 'page' );
  11.     foreach ($screens as $screen) {
  12.         add_meta_box(
  13.             'myplugin_sectionid',
  14.             __( 'My Post Section Title', 'myplugin_textdomain' ),
  15.             'myplugin_inner_custom_box',
  16.             $screen
  17.         );
  18.     }
  19. }

  20. /* 输出模块内容 */
  21. function myplugin_inner_custom_box( $post ) {

  22.   // 使用随机数进行核查
  23.   wp_nonce_field( plugin_basename( __FILE__ ), 'myplugin_noncename' );

  24.   // 用于数据输入的实际字段
  25.   // 使用 get_post_meta 从数据库中检索现有的值,并应用到表单中
  26.   $value = get_post_meta( $post->ID, '_my_meta_value_key', true );
  27.   echo '<label for="myplugin_new_field">';
  28.        _e("Description for this field", 'myplugin_textdomain' );
  29.   echo '</label> ';
  30.   echo '<input type="text" id="myplugin_new_field" name="myplugin_new_field" value="'.esc_attr($value).'" size="25" />';
  31. }

  32. /* 文章保存时,保存我们的自定义数据*/
  33. function myplugin_save_postdata( $post_id ) {

  34.   // 首先,我们需要检查当前用户是否被授权做这个动作。
  35.   if ( 'page' == $_POST['post_type'] ) {
  36.     if ( ! current_user_can( 'edit_page', $post_id ) )
  37.         return;
  38.   } else {
  39.     if ( ! current_user_can( 'edit_post', $post_id ) )
  40.         return;
  41.   }

  42.   // 其次,我们需要检查,是否用户想改变这个值。
  43.   if ( ! isset( $_POST['myplugin_noncename'] ) || ! wp_verify_nonce( $_POST['myplugin_noncename'], plugin_basename( __FILE__ ) ) )
  44.       return;

  45.   // 第三,我们可以保存值到数据库中

  46.   //如果保存在自定义的表,获取文章ID
  47.   $post_ID = $_POST['post_ID'];
  48.   //过滤用户输入
  49.   $mydata = sanitize_text_field( $_POST['myplugin_new_field'] );

  50.   // 使用$mydata做些什么
  51.   // 或者使用
  52.   add_post_meta($post_ID, '_my_meta_value_key', $mydata, true) or
  53.     update_post_meta($post_ID, '_my_meta_value_key', $mydata);
  54.   // 或自定义表(见下面的进一步阅读的部分)
  55. }
  56. ?>
复制代码


  1. /**
  2. * 在文章编辑界面调用这个类
  3. */
  4. function call_someClass()
  5. {
  6.     return new someClass();
  7. }
  8. if ( is_admin() )
  9.     add_action( 'load-post.php', 'call_someClass' );

  10. /**
  11. * 这个类
  12. */
  13. class someClass
  14. {
  15.     const LANG = 'some_textdomain';

  16.     public function __construct()
  17.     {
  18.         add_action( 'add_meta_boxes', array( &$this, 'add_some_meta_box' ) );
  19.     }

  20.     /**
  21.      * 添加Meta模块
  22.      */
  23.     public function add_some_meta_box()
  24.     {
  25.         add_meta_box(
  26.              'some_meta_box_name'
  27.             ,__( 'Some Meta Box Headline', self::LANG )
  28.             ,array( &$this, 'render_meta_box_content' )
  29.             ,'post'
  30.             ,'advanced'
  31.             ,'high'
  32.         );
  33.     }


  34.     /**
  35.      * 呈送Meta模块内容
  36.      */
  37.     public function render_meta_box_content()
  38.     {
  39.         echo '<h1>TEST OUTPUT - this gets rendered inside the meta box.</h1>';
  40.     }
  41. }
复制代码


回调数组
$callback_args 数组将被传递给回调函数的第二个参数。第一个参数是这篇文章的 $post 对象。

  1. // 这个函数添加一个带有回调函数 my_metabox_callback() 的Meta模块
  2. function add_my_meta_box() {
  3.      $var1 = 'this';
  4.      $var2 = 'that';
  5.      add_meta_box(
  6.            'metabox_id',
  7.            'Metabox Title',
  8.            'my_metabox_callback',
  9.            'page',
  10.            'normal',
  11.            'low',
  12.            array( 'foo' => $var1, 'bar' => $var2)
  13.       );
  14. }

  15. // $post 是一个包含当前文章的对象 (作为一个 $post 对象)
  16. // $metabox 是一个数组,包含模块 id, title, callback, and args elements.
  17. // args element 是一个包含传递到 $callback_args 变量的数组

  18. function my_metabox_callback ( $post, $metabox ) {
  19.      echo 'Last Modified: '.$post->post_modified;        // 输出文章最后编辑的时间
  20.      echo $metabox['args']['foo'];                         // 输出 'this'
  21.      echo $metabox['args']['bar'];                         // 输出 'that'
  22.      echo get_post_meta($post->ID,'my_custom_field',true); // 输出自定义字段的值
  23. }
复制代码
回复

使用道具 举报

670

主题

785

帖子

8273

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
8273
沙发
 楼主| 发表于 2021-6-13 08:49:41 | 只看该作者
例子:

  1. //在后台添加一个备注元数据框
  2. //需要给 add_meta_boxes钩子,挂载一个自定义方法
  3. add_action('add_meta_boxes','myplugin_add_meta_box');


  4. //添加一个元数据框到post和page的管理界面中
  5. function myplugin_add_meta_box(){
  6.         $screens = array('post','page');
  7.         add_meta_box(
  8.                 'myplugin_sectionid-test',//整个元数据框的id值,不要重复,方便JS二次修改
  9.                 '文章备注',//数据框的标题,也可以用_e多语言方式
  10.                 'myplugin_meta_box_callback',//回调函数
  11.                 $screens
  12.         );
  13. }

  14. //回调函数 读取及展示数据
  15. function myplugin_meta_box_callback($post){
  16.         //添加一个验证信息,在保存元数据时用
  17.         wp_nonce_field('myplugin_save_meta_box_data','myplugin_meta_box_nonce');//在元数据框中添加一个隐藏input,id和name都是myplugin_meta_box_nonce,value为随机,下方保存数据时用于判断此表单是否存在
  18.         $value = get_post_meta($post->ID,'_qssdata',true);//数据库取值,字段名为_qssdata
  19.         //echo '<label for="myplugin_new_field">我是描述</label>';
  20.         echo '<textarea id="_qssdata" name="_qssdata" style="width:100%;height:100px;">'.esc_attr($value).'</textarea>';
  21.         //echo '<input type="text" id="_qssdata" name="_qssdata" value="'.esc_attr($value).'" size="25" />';
  22. }

  23. //保存数据
  24. add_action('save_post','myplugin_save_meta_box_data');
  25. function myplugin_save_meta_box_data($post_id){
  26.         //验证来源
  27.         if(!isset($_POST['myplugin_meta_box_nonce'])){return;}
  28.         if(!wp_verify_nonce($_POST['myplugin_meta_box_nonce'],'myplugin_save_meta_box_data')){return;}
  29.         //自动保存版本,不进行操作
  30.         if(defined('DOING_AUTOSAVE') && DOING_AUTOSAVE){return;}
  31.         //验证操作者权限
  32.         if(isset($_POST['post_type']) && 'page'==$_POST['post_type']){
  33.                 if(!current_user_can('edit_page',$post_id)){return;}
  34.         }else{
  35.                 if(!current_user_can('edit_page',$post_id)){return;}
  36.         }
  37.         //判断有没有数据传入
  38.         if(!isset($_POST['_qssdata'])){return;}
  39.         //序列化传入的数据
  40.         $my_data = sanitize_text_field( $_POST['_qssdata'] );
  41.         //操作数据库 保存名称为_qssdata
  42.         update_post_meta( $post_id, '_qssdata' , $my_data);
  43. }
复制代码

回复 支持 反对

使用道具 举报

670

主题

785

帖子

8273

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
8273
板凳
 楼主| 发表于 2021-6-13 18:36:56 | 只看该作者
前台调用这个例子
echo get_post_meta($post->ID,'_qssdata',true)
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|5ucms.com

GMT+8, 2024-6-16 23:36 , Processed in 0.046875 second(s), 28 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表