I am adding a meta box in the create post/page interface and I want to get the ID of the post being edited/created so I can dynamically display the value of the input field.

From the WordPress codex :

/* Prints the box content */
function myplugin_inner_custom_box() {

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

  // The actual fields for data entry
  echo '<label for="myplugin_new_field">' . __("Description for this field", 'myplugin_textdomain' ) . '</label> ';
  echo '<input type="text" id= "myplugin_new_field" name="myplugin_new_field" value="whatever" size="25" />';
}

How do I pass the ID to myplugin_inner_custom_box()? So I can use the following within it:

// Get the value of the meta key that is associated to the page
  $as_meta_value = get_post_meta( $post_id, 'as_link_to_image', true );

and replace the whatever value with the value of the meta key in the input field.

2 Answers
2

Try get_the_ID() or global $post; $post->ID.

Leave a Reply

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