WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Data in custom meta box not saved (3 posts)

  1. spstieng
    Member
    Posted 3 years ago #

    I had it working, but now it's not working any more.

    I've added some custom metaboxes to my edit post page and I have the following problems:

    1. Meta data not stored (was working)
    2. Medta not deleted when empty (was not working wen 1. was working).

    Is there something wrong with my code?

    I got the following code in my functions.php file:

    add_action( 'admin_menu', 'create_custom_meta_box' );
    add_action( 'save_post', 'save_custom_post_meta_box', 10, 2 );
    
    function create_custom_meta_box() {
      add_meta_box( 'Intro', __('Intro'), 'post_intro_meta_box', 'post', 'normal', 'high' );
    }
    
    function save_custom_post_meta_box( $post_id, $post ) {
    
      if ( !current_user_can( 'edit_post', $post_id ) )
    		return $post_id; 
    
      // Custom intro text box (Ingress)
      // If custom field does not exists, it is created.
      // Then add, update and delete handles are added
      /*--------------------------------------------------------------------------- */  
    
      if ( !wp_verify_nonce( $_POST['post_intro_meta_box_nonce'], plugin_basename( __FILE__ ) ) )
    		return $post_id;
      // Mini intro
      $meta_value = get_post_meta( $post_id, '_post_mini_intro', true );
      $new_meta_value = stripslashes( $_POST['post_mini_intro'] );
    
      if ( $new_meta_value && '' == $meta_value )
    	add_post_meta( $post_id, '_post_mini_intro', $new_meta_value, true );
    
      elseif ( $new_meta_value != $meta_value )
    	update_post_meta( $post_id, '_post_mini_intro', $new_meta_value );
    
      elseif ( '' == $new_meta_value && $meta_value )
    	delete_post_meta( $post_id, '_post_mini_intro', $meta_value );
    }
  2. spstieng
    Member
    Posted 3 years ago #

    I just saw that auto save might cause problems. So I have added the following code:

    if (!defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
        return $post_id;
  3. spstieng
    Member
    Posted 3 years ago #

    I'm not sure what caused this problem for me. But I have opened a earlier saving of the code and it's working now.

Topic Closed

This topic has been closed to new replies.

About this Topic