I'm working on a plugin that takes info typed into a field (inside a meta box on the Edit Post page), and saves it with the post. More or less like a custom field.
Code is all taken from examples on wordpress.org.
Anyway, I can't seem to save the content of the field into the post's meta data! Help!
<?php
/*
Plugin Name: Routines List Panel
Plugin URI: http://www.roestudios.com/
Description: Adds a panel with dropdown for routines.
Author: Benjamin Allison
Version: 1.0
Author URI: http://www.roestudios.com/
*/
/* Use the admin_menu action to define the custom boxes */
add_action('admin_menu', 'myplugin_add_custom_box');
/* Use the save_post action to do something with the data entered */
add_action('save_post', 'myplugin_save_postdata');
/* Adds a custom section to the "advanced" Post and Page edit screens */
function myplugin_add_custom_box() {
if( function_exists( 'add_meta_box' )) {
add_meta_box( 'myplugin_sectionid', __( 'Routines List', 'myplugin_textdomain' ),
'myplugin_inner_custom_box', 'post', 'advanced' );
} else {
add_action('dbx_post_advanced', 'myplugin_old_custom_box' );
}
}
/* Prints the inner fields for the custom post/page section */
function myplugin_inner_custom_box() {
// Use nonce for verification
echo '<input type="hidden" name="myplugin_noncename" id="myplugin_noncename" value="' .
wp_create_nonce( plugin_basename(__FILE__) ) . '" />';
// The actual fields for data entry
echo '<label for="myplugin_new_field">' . __("Select a routine for this client <br /><br />", 'myplugin_textdomain' ) . '</label> ';
$yeye = get_post_meta( $post_id, 'routine_selection' );
echo '<input type="text" name="myplugin_new_field" value="'.$yeye.'" size="25" />';
}
/* When the post is saved, saves our custom data */
function myplugin_save_postdata( $post_id ) {
// 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 $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 $post_id;
// Check permissions
if ( 'page' == $_POST['post_type'] ) {
if ( !current_user_can( 'edit_page', $post_id ) )
return $post_id;
} else {
if ( !current_user_can( 'edit_post', $post_id ) )
return $post_id;
}
// OK, we're authenticated: we need to find and save the data
$mydata = $_POST['myplugin_new_field'];
add_post_meta($post_id, 'routine_selection', $mydata, true) or update_post_meta($post_id, 'routine_selection', $mydata);
return true;
// Do something with $mydata
// probably using add_post_meta(), update_post_meta(), or
// a custom table (see Further Reading section below)
}
?>