如何在帖子页面中创建自定义面板和字段[插件]

时间:2013-01-25 作者:Vallieres

我想写一个自定义插件,在post compose面板下面添加一个自定义面板,该面板将有几个字段和一个submit按钮。JS将开始向数据库提交内容,而不会保存或干扰后期保存/提交机制。

目的是保存要使用的数据,并在撰写帖子时快速访问。我可以做一个插件设置页面,但我不喜欢在同一个地方使用两个屏幕的想法:)

我看到了自定义字段插件,但它们为帖子增加了价值,而我希望在写帖子时创建一些更灵活的内容,但不一定链接到此帖子。

我似乎找不到该怎么做。。。

谢谢

1 个回复
最合适的回答,由SO网友:Bainternet 整理而成

在WordPress世界中,它被称为“元框”,在您的情况下,它与帖子相同。您需要做的唯一不同的是保存功能,该功能应将数据保存在选项表中,下面是一个修改后的codex示例,该示例应作为您的起点:

<?php
/* Define the custom box */
add_action( \'add_meta_boxes\', \'myplugin_add_custom_box_WPA83147\' );

/* Adds a box to the main column on the Post and Page edit screens */
function myplugin_add_custom_box_WPA83147() {
  add_meta_box( 
      \'myplugin_sectionid\',
      __( \'My Post Section Title\', \'myplugin_textdomain\' ),
      \'myplugin_inner_custom_box_WPA83147\',
      \'post\' 
  );
}

/* Prints the box content */
function myplugin_inner_custom_box_WPA83147( $post ) {

  // Use nonce for verification
  wp_nonce_field( plugin_basename( __FILE__ ), \'myplugin_noncename_WPA83147\' );

  // The actual fields for data entry
  // Use get_option to retrieve an existing value from the database and use the value for the form
  $options = get_option(\'_WPA83147_options\', array());
  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="\'.(isset($options[\'myplugin_new_field\']) ? $options[\'myplugin_new_field\'] : "").\'" size="25" />\';
}


/* Do something with the data entered */
add_action( \'save_post\', \'myplugin_save_postdata_WPA83147\' );
/* When the post is saved, saves our custom data */
function myplugin_save_postdata_WPA83147( $post_id ) {
  // verify if this is an auto save routine. 
  // If it is our form has not been submitted, so we dont want to do anything
  if ( defined( \'DOING_AUTOSAVE\' ) && DOING_AUTOSAVE ) 
      return;

  // verify this came from the our screen and with proper authorization,
  // because save_post can be triggered at other times

  if ( !wp_verify_nonce( $_POST[\'myplugin_noncename_WPA83147\'], plugin_basename( __FILE__ ) ) )
      return;


  // Check permissions
  if ( \'page\' == $_POST[\'post_type\'] ) 
  {
    if ( !current_user_can( \'edit_page\', $post_id ) )
        return;
  }
  else
  {
    if ( !current_user_can( \'edit_post\', $post_id ) )
        return;
  }

  // OK, we\'re authenticated: we need to find and save the data
  if (isset($_POST[\'myplugin_new_field\'])){
    //sanitize user input
    $mydata = sanitize_text_field( $_POST[\'myplugin_new_field\'] ); 
    //get all saved options
    $data = get_option(\'_WPA83147_options\', array());
    //updated the field you need
    $data[\'myplugin_new_field\'] = $mydata;
    //store in the database
    update_option(\'_WPA83147_options\', $$data);
  }

}

结束

相关推荐

使用AJAX时WP_QUERY ORDERBY中断?

我遇到了一点问题。我有一个函数叫做get_press(), 它检索最新的新闻项目。它位于插件内部:class EWPress { function __construct() { load_plugin_textdomain( \'ew\', flase, dirname( plugin_basename( __FILE__ ) ) . \'/lang\' ); add_action( \'wp_enqueue_sc