Add field to post/page edit form

24th Jun 2011 at 10:12 AM | Posted in Wordpress | 6 Comments
<?php
//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() {
    add_meta_box(
        'myplugin_sectionid',
        __( 'Custom Text for Post', 'myplugin_customtext' ),
        'myplugin_inner_custom_box',
        'post'
    );
    add_meta_box(
        'myplugin_sectionid',
        __( 'Custom Text for Page', 'myplugin_customtext' ),
        'myplugin_inner_custom_box',
        'page'
    );
}
/* 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() {
    jQuery("#myplugin_description").addClass("mceEditor");
    if ( typeof( tinyMCE ) == "object" && typeof( tinyMCE.execCommand ) == "function" ) {
    tinyMCE.execCommand("mceAddControl", true, "myplugin_description");
    }
    });
  </script>
<?php
}
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 )
      return;

  // 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__ ) ) )
      return;

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

  // 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;
}
?>

Output:

Advertisements

6 Comments »

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.
    Cheers

  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:

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s

Blog at WordPress.com.
Entries and comments feeds.

%d bloggers like this: