Add Private Note Fields In Post Edit Page For WordPress

目錄

Add Private Note Field In Post Edit Page For WordPress

First, let’s add a meta box in post edit page, like the Custom Pos Options. You can find I customized several options and gadgets. Yap, I am a lazy guy.

add_action( 'add_meta_boxes', 'custom_post_option_meta_box' );
function custom_post_option_meta_box() {
    add_meta_box(
        'custom_post_option_meta_group',
        'Custom Post Options',
        'custom_post_option_meta_box_callback',
        'post'
    );
}

After adding a meta box, we add callback further. Callback means the content for the meta box.

function custom_post_option_meta_box_callback($post){

  $post_id = $post->ID; ?>

  <?php $modified_date_switch = get_post_meta($post_id,'show_modified_date', true ); ?>
  <div class="meta_box_item">
    <label>Show Modified Date</label>
    <input type="checkbox" name="show_modified_date" <?php echo ($modified_date_switch) ? 'checked' : ''; ?>>
  </div>

  <?php $private_note = get_post_meta( $post_id, '_private_note', true ); ?>
  <div class="meta_box_item">
    <label>Private Note</label>
    <textarea id="private_note" name="private_note"><?php echo $private_note; ?></textarea>
  </div>
  <?php
}

Finally, we save the value of custom filed as a post meta. Without this, all your content won’t be written to database.

add_action('save_post', 'save_post_private_note');
function save_post_private_note( $post_id ) {
  if ( isset($_POST['private_note']) ) {
		update_post_meta($post_id, '_private_note', esc_textarea($_POST['private_note']));
  }
}

Related Posts

The Complete WordPress Customization Tutorial – Just Copy And Paste And Work

Content Protection by DMCA.com
A
Aron

Leave a Reply

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