Add field to post/page edit form

24th Jun 2011 at 10:12 AM | Posted in Wordpress | 6 Comments
//hook admin edit page
add_action( 'add_meta_boxes', 'myplugin_add_custom_box' );
//save data
add_action( 'save_post', 'myplugin_save_postdata' );

/* Adds a box to the main column on the Post and Page edit screens */
function myplugin_add_custom_box() {
        __( 'Custom Text for Post', 'myplugin_customtext' ),
        __( 'Custom Text for Page', 'myplugin_customtext' ),
/* Prints the box content */
function myplugin_inner_custom_box() {
  // Use nonce for verification
  wp_nonce_field( plugin_basename( __FILE__ ), 'myplugin_noncename' );
	global $post;
	$myplugin_customtext = get_post_meta($post->ID,'myplugin_customtext',true);
	$myplugin_description = get_post_meta($post->ID,'myplugin_description',true);
  // The actual fields for data entry
  echo '<p><label for="myplugin_new_field">';
       _e("Title", 'myplugin_customtext' );
  echo '</label></p>';
  echo '<div><input type="text" id="myplugin_customtext" name="myplugin_customtext" value="'.$myplugin_customtext.'" size="25" style="width:100%;" /></div>';

  echo '<p><label for="myplugin_new_field">';
       _e("Description", 'myplugin_customtext' );
  echo '</label></p>';
  echo '<div style="border:1px solid #DFDFDF;"><textarea id="myplugin_description" name="myplugin_description">'.$myplugin_description.'</textarea></div>';
  <script type="text/javascript">
    jQuery(document).ready(function() {
    if ( typeof( tinyMCE ) == "object" && typeof( tinyMCE.execCommand ) == "function" ) {
    tinyMCE.execCommand("mceAddControl", true, "myplugin_description");
function myplugin_save_postdata( $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 )

  // 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['myplugin_noncename'], plugin_basename( __FILE__ ) ) )

  // Check permissions
  if ( 'page' == $_POST['post_type'] )
    if ( !current_user_can( 'edit_page', $post_id ) )
    if ( !current_user_can( 'edit_post', $post_id ) )

  // OK, we're authenticated: we need to find and save the data
	$myplugin_customtext = $_POST['myplugin_customtext'];
	$myplugin_description = $_POST['myplugin_description'];

	 update_post_meta($post_id, "myplugin_customtext", $myplugin_customtext);
	 update_post_meta($post_id, "myplugin_description", $myplugin_description);
  // Do something with $mydata
  // probably using add_post_meta(), update_post_meta(), or
  // a custom table (see Further Reading section below)
   return true;




RSS feed for comments on this post. TrackBack URI

  1. Excellent blog you have here.. It’s hard to find quality writing like yours nowadays. I honestly appreciate people like you! Take care!!

  2. Hi there just wanted to give you a quick heads up.

    The text in your post seem to be running off the screen
    in Firefox. I’m not sure if this is a format issue or something to do with internet browser compatibility but I thought I’d post to let you know.
    The layout look great though! Hope you get the problem resolved soon.

  3. Wow that was odd. I just wrote an extremely long comment but after I clicked submit my comment didn’t show up. Grrrr… well I’m
    not writing all that over again. Anyways, just wanted to say wonderful blog!

  4. Aw, this was a very good post. Taking a few minutes and actual effort
    to produce a great article… but what can I say… I
    put things off a whole lot and don’t seem to get nearly anything done.

  5. Pretty! This has been an incredibly wonderful post.
    Thank you for providing this info.

  6. Hey! I realize this is sort of off-topic but I needed to
    ask. Does building a well-established website like yours take a large amount
    of work? I am completely new to writing a blog however
    I do write in my diary every day. I’d like to start a blog so I can share my personal experience and thoughts online. Please let me know if you have any ideas or tips for new aspiring bloggers. Appreciate it!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

Blog at
Entries and comments feeds.

%d bloggers like this: