0

I followed this tutorial and got everything to display and save properly, however I wanted to create a new class for this to clean up the code. Unfortunately after doing so the data no longer saves and I haven't been able to fix the issue.

http://code.tutsplus.com/tutorials/reusable-custom-meta-boxes-part-1-intro-and-basic-fields--wp-23259

The meta box fields are output from a switch statement based on the contents of the $custom_meta_fields array.

class VPC_Customization_Meta_Boxes {

protected $custom_meta_fields = array();

function __construct() {
    add_action( 'add_meta_boxes', array( $this, 'create_fields_array') );
    add_action( 'load-post.php', array( $this,'setup_boxes') );
    add_action( 'load-post-new.php', array( $this, 'setup_boxes') );
    add_action( 'admin_enqueue_scripts', array($this, 'load_scripts') ); 
    /* Create fields array on the 'add_meta_boxes' hook */

}

/* Meta box setup function. */
function setup_boxes() {

  /* Add meta boxes on the 'add_meta_boxes' hook. */
  add_action( 'add_meta_boxes', array( $this, 'add_boxes') );
  /* Save post meta on the 'save_post' hook. */
  add_action('save_post', array( $this, 'save_meta'));
}

...

// Save the Data
function save_meta($post_id) {

    // verify nonce
    if (!wp_verify_nonce($_POST['vpc_customization_meta_box_nonce'], basename(__FILE__))) 
        return;
    // check autosave
    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
        return;
    // check permissions
    $post_type_slug = get_post_type( $post_id );
    $post_type = get_post_type_object( $post_type_slug );

    if( $post_type_slug === $_POST['post_type'] ){
        if( !current_user_can( $post_type->cap->edit_post, $post_id ) ) 
            return;
    }

    // loop through fields and save the data
    foreach ($this->custom_meta_fields as $field) {
        $new = false;
        $old = get_post_meta($post_id, $field['id'], true);
        if ( isset( $_POST[$field['id']] ) )
            $new = $_POST[$field['id']];

        if ( isset( $new ) && $new != $old ) {
            update_post_meta( $post_id, $field['id'], $new );
        } 
        elseif ( isset( $new ) && '' == $new && $old ) {
            delete_post_meta( $post_id, $field['id'], $old );
        }
    } // end foreach
}

One thing I'm not sure of is if I'm accessing $post and $post_ID properly from within in the class.

Full code: http://pastebin.com/rxnyeRar

Thanks in advance for your help!

0

At first glance it looks like you need to add your save routine to a hook, which in this case would save_post.

function __construct() {
    add_action( 'add_meta_boxes', array( $this, 'create_fields_array') );
    add_action( 'load-post.php', array( $this,'setup_boxes') );
    add_action( 'load-post-new.php', array( $this, 'setup_boxes') );
    add_action( 'admin_enqueue_scripts', array($this, 'load_scripts') ); 

    //don't forget the save routine
    add_action( 'save_post', array($this, 'save_meta') ); 

}

This is making the assumption that your save_meta() function is in the class and that your class is being properly instantiated.

  • Hi Helga! Thanks for the quick response, I have the 'save_post' hook in the setup_boxes() function, my bad for not including that originally. If possible, would you be able to take a look at my pastebin? – Ray J Aug 14 '14 at 20:35
  • I would remove the setup_boxes method completely and move both its functions into the contructor. See if that helps. – helgatheviking Aug 14 '14 at 22:43
  • Unfortunately the data still isn't saving, but thank you. – Ray J Aug 14 '14 at 23:24
  • Ok, but that helps us narrow down the source of the problem. I'm going to bed, but I am going to guess the problem is with your Nonce. What I sometimes do is replace all the return statements with wp_die("this is the reason we failed here" type messages that help you pinpoint where the save routine is failing. – helgatheviking Aug 14 '14 at 23:30
  • Thank you for the suggestion to use wp_die, it turns out that within the save_meta() method I'm unable to access the $custom_meta_fields array. I haven't been able to figure out why yet. – Ray J Aug 15 '14 at 16:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.