WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Custom MetaBox Data Not Being Added (3 posts)

  1. Howdy_McGee
    Member
    Posted 10 months ago #

    I can't figure it out - it won't add anythign to the database even though there is 100% a value being passed into $_POST['_desc'] what am I doing wrong?

    /** Add the Meta Box **/
    function add_custom_meta_box() {
    	global $meta_box;
        add_meta_box(
    		'short-desc', // $id
    		'Short Description', // $title
    		'show_custom_meta_box', // $callback
    		'post', // $page
    		'side', // $context
    		'high'); // $priority
    }
    add_action('add_meta_boxes', 'add_custom_meta_box');
    
    /** The Callback **/
    function show_custom_meta_box() {
    	global $post;
    	// Use nonce for verification
    	echo '<input type="hidden" name="shortdesc_meta_box_nonce" value="'.wp_create_nonce(basename(__FILE__)).'" />';
    
    	// get value of this field if it exists for this post
    	$meta = get_post_custom($post->ID);
    	echo var_dump($meta);
    
    	// Begin the field table and loop
    	echo '<table class="form-table"><tr><td>';
    
    		echo '<strong>Enter A Short Description:</strong>
    			<input type="text" name="_desc" id="short-desc" value="'.$meta['_desc'].'" size="30" />';
    
    	echo '</td></tr></table>'; // end table
    } 
    
    /** Save the Data **/
    function save_custom_meta($post) {
    	// verify nonce
    	if (!wp_verify_nonce($_POST['shortdesc_meta_box_nonce'], basename(__FILE__)))
    		return $post->ID;
    	// check autosave
    	if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
    		return $post->ID;
    	// check permissions
    	if (!current_user_can('edit_post', $post->ID))
    		return $post->ID;
    
    	echo $_POST['_desc'];
    //	die();
    
    	if(isset($_POST['_desc']))
    		update_post_meta($post->ID, '_desc', strip_tags($_POST['_desc']));
    }
    add_action('save_post', 'save_custom_meta', 1, 2);
  2. bcworkz
    Member
    Posted 10 months ago #

    You are not collecting the passed 'save_post' parameters correctly. Use this:
    function save_custom_meta($id, $post) {

    Then you can use either $id or $post->ID for the post ID.

  3. Howdy_McGee
    Member
    Posted 10 months ago #

    I ended up passing it as function save_custom_meta($post_id){} and it worked great. Thanks!

Reply

You must log in to post.

About this Topic