add custom meta to post

Add custom meta box to the post.

* Define the custom box *
add_action( \'add_meta_boxes\', \'add_author_box\' );
add_action( \'save_post\', \'author_save_postdata\' );

* Adds a box to the main column on the Post edit screens *
function add_author_box() {
		add_meta_box(\'wpt_authors\', \'Author Custom\', \'author_inner_custom_box\', \'post\');
}

* Prints the box content *
function author_inner_custom_box( $post ) {
  wp_nonce_field( plugin_basename( __FILE__ ), \'author_noncename\' );
  	$value = get_post_meta( $post->ID, \'_newauthor\', true );
	echo \'

Enter the authors names:

\'; echo \'\'; } * When the post is saved, saves our custom data * function author_save_postdata( $post_id ) { First we need to check if the current user is authorised to do this action. if ( \'page\' == $_POST[\'post_type\'] ) { if ( ! current_user_can( \'edit_page\', $post_id ) ) return; } else { if ( ! current_user_can( \'edit_post\', $post_id ) ) return; } Secondly we need to check if the user intended to change this value. if ( ! isset( $_POST[\'author_noncename\'] ) || ! wp_verify_nonce( $_POST[\'author_noncename\'], plugin_basename( __FILE__ ) ) ) return; Thirdly we can save the value to the database if saving in a custom table, get post_ID $post_ID = $_POST[\'post_ID\']; sanitize user input $mydata = sanitize_text_field( $_POST[\'new_author\'] ); Do something with $mydata either using add_post_meta($post_ID, \'_newauthor\', $mydata, true) or update_post_meta($post_ID, \'_newauthor\', $mydata); or a custom table (see Further Reading section below) }