为新的WordPress帖子添加“帖子选项”

时间:2011-12-06 作者:user771417

如何在所见即所得编辑器下添加“Post Options”部分?Like this

2 个回复
最合适的回答,由SO网友:Juan Ramón 整理而成
SO网友:Joshua Abenazer

请尝试以下代码以开始。

/* Define the custom box */

// WP 3.0+
add_action( \'add_meta_boxes\', \'post_options_metabox\' );

// backwards compatible
add_action( \'admin_init\', \'post_options_metabox\', 1 );

/* Do something with the data entered */
add_action( \'save_post\', \'save_post_options\' );

/**
 *  Adds a box to the main column on the Post edit screen
 * 
 */
function post_options_metabox() {
    add_meta_box( \'post_options\', __( \'Post Options\' ), \'post_options_code\', \'post\', \'normal\', \'high\' );
}

/**
 *  Prints the box content
 */
function post_options_code( $post ) { 
    wp_nonce_field( plugin_basename( __FILE__ ), $post->post_type . \'_noncename\' );
    $meta_info = get_post_meta( $post->ID, \'_meta_info\', true) ? get_post_meta( $post->ID, \'_meta_info\', true) : 1; ?>
    <h2><?php _e( \'Meta Information\' ); ?></h2>
    <div class="alignleft">
        <input id="meta_default" type="radio" name="_meta_info" value="meta_default"<?php checked( \'meta_default\', $meta_info ); ?><?php echo ( $meta_info == 1 )?\' checked="checked"\' : \'\'; ?> /> <label for="meta_default" class="selectit"><?php _e( \'Use default post setting\' ); ?></label><br />
        <input id="show_meta" type="radio" name="_meta_info" value="show_meta"<?php checked( \'show_meta\', $meta_info ); ?> /> <label for="show_meta" class="selectit"><?php _e( \'Show meta info\' ); ?></label><br />
        <input id="hide_meta" type="radio" name="_meta_info" value="hide_meta"<?php checked( \'hide_meta\', $meta_info ); ?> /> <label for="hide_meta" class="selectit"><?php _e( \'Hide meta info\' ); ?></label><br />
    </div>
    <div class="alignright">
        <span class="description"><?php _e( \'Your explanation here\' ); ?></span>
    </div>
    <div class="clear"></div>
    <hr /><?php
}

/** 
 * When the post is saved, saves our custom data 
 */
function save_post_options( $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[$_POST[\'post_type\'] . \'_noncename\'], plugin_basename( __FILE__ ) ) )
      return;

  // Check permissions
  if ( !current_user_can( \'edit_post\', $post_id ) )
     return;

  // OK, we\'re authenticated: we need to find and save the data
  if( \'post\' == $_POST[\'post_type\'] ) {
      if ( !current_user_can( \'edit_post\', $post_id ) ) {
          return;
      } else {
          update_post_meta( $post_id, \'_meta_info\', $_POST[\'_meta_info\'] );
      }
  } 

}

结束