• Resolved dguyen

    (@dguyen)


    Completely frustrated at this point with meta boxes. Using the WP example in the codex to create a simple text input meta box on both the write post & write page and the issue is that no value is saved when updating!

    I’ve completely gone primal, testing on a 2010 child theme with only a functions.php file with

    /* Define the custom box */
    
    // WP 3.0+
    // add_action('add_meta_boxes', 'myplugin_add_custom_box');
    
    // backwards compatible
    add_action('admin_init', 'myplugin_add_custom_box', 1);
    
    /* Do something with the data entered */
    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',
            __( 'My Post Section Title', 'myplugin_textdomain' ),
            'myplugin_inner_custom_box',
            'post'
        );
        add_meta_box(
            'myplugin_sectionid',
            __( 'My Post Section Title', 'myplugin_textdomain' ),
            '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' );
    
      // The actual fields for data entry
      echo '<label for="myplugin_new_field">';
           _e("Description for this field", 'myplugin_textdomain' );
      echo '</label> ';
      echo '<input type="text" id="myplugin_new_field" name="myplugin_new_field" value="whatever" 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'];
    
      // Do something with $mydata
      // probably using add_post_meta(), update_post_meta(), or
      // a custom table (see Further Reading section below)
    
       return $mydata;
    }

    Straight from the codex and the value doesn’t want to save… any suggestions?

Viewing 2 replies - 1 through 2 (of 2 total)
  • it’s not saving because your save function doesn’t actually do anything.

    // Do something with $mydata
      // probably using add_post_meta(), update_post_meta(), or
      // a custom table (see Further Reading section below)

    You’re supposed to customize that section to do something.

    http://www.deluxeblogtips.com/2010/04/how-to-create-meta-box-wordpress-post.html

    Thread Starter dguyen

    (@dguyen)

    Thanks fonglh… How could I have missed that.

    I originally used the example provided by dbt but thought I could reduce the excess code to write something more simple according to my needs.

    I guess I’ll just commit to using dbt’s new version 3.01. Thanks

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Meta Box values not saving’ is closed to new replies.