Skip to content

WordPress – Add options to post admin ‘Publish’ Meta Box

Screenshot from 2015-09-22 16:22:18
This function will add content to the ‘Publish’ meta box in the post edit screen. You can use it to add additional meta fields or just simply some text.

function add_publish_meta_options($post_obj) {
 
  global $post;
  $post_type = 'freelancer-doc'; // If you want a specific post type
  $value = get_post_meta($post_obj->ID, 'check_meta', true); // If saving value to post_meta
 
  if($post_type==$post->post_type) {
    echo  '<div class="misc-pub-section misc-pub-section-last">'
         .'<label><input type="checkbox"' . (!empty($value) ? ' checked="checked" ' : null) . ' value="1" name="check_meta" /> Check meta</label>'
         .'</div>';
  }
}
 
/*
 * Add the extra options to the 'Publish' box
 */
add_action('post_submitbox_misc_actions', 'add_publish_meta_options');

If you want to save the values in the fields…

function extra_publish_meta_options_save($post_id, $post, $update) {
 
  /*
   * If using specific post type
   */
  $post_type = 'post';
  if ( $post_type != $post->post_type ) {
    return;
  }
 
  if ( wp_is_post_revision( $post_id ) ) {
    return;
  }
 
  /*
   * Checkbox value is 1 if set
   */
  if(isset($_POST['check_meta']) && $_POST['check_meta'] == 1) {
    update_post_meta($post_id, 'check_meta', $_POST['check_meta']);
  }
 
 
}
 
/*
 * Init extra_publish_options_save() on save_post action
 */
add_action( 'save_post', 'extra_publish_options_save', 10 , 3);

Credit to http://stackoverflow.com/questions/9907858/how-to-add-a-field-in-edit-post-page-inside-publish-box-in-wordpress

About 

10 years + experience in web development working with lots of different technology.

Published inPHPWordPress

One Comment

  1. dineshkashera dineshkashera

    Hello ,
    you just call the wrong function on save_post hook. Please update the function name .

Leave a Reply

Your email address will not be published. Required fields are marked *