0

I have just started working with custom post types and meta fields/data. I'm able to get the custom post and meta field to appear. It just won't save. I've been staring at the code and reading up endlessly but nothing has jogged for me what is wrong. I would love some fresh eyes on it to see what y'all think. Once I have this piece figured out I'm certain I will be able to scale the use of these functions.

Here's the code I have for my meta box:

/*Custom META Boxes for Ranch Custom Post Types*/
/*Ranch Location META Box*/
add_action( 'add_meta_boxes', 'ranch_location_meta' );
function ranch_location_meta() {
add_meta_box(
    'ranch_location_meta',
    __( 'Ranch Location', 'myplugin_textdomain' ),
    'ranch_location_meta_content',
    'ranch',
    'side',
    'high'
);
}

function ranch_location_meta_content( $post ) {
    wp_nonce_field( plugin_basename( __FILE__ ), 'ranch_location_meta_content_nonce' );
    echo '<label for="ranch_location"></label>';
    echo '<input type="text" id="ranch_location" name="ranch_location"     placeholder="Enter  Ranch Location Here" />';
}

add_action( 'save_post', 'ranch_location_save' );
function ranch_location_save( $post_id ) {

    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
        return;

    if ( !wp_verify_nonce( $_POST['ranch_location_meta_content_nonce'], plugin_basename( __FILE__ ) ) )
        return;

    if ( 'page' == $_POST['post_type'] ) {
        if ( !current_user_can( 'edit_page', $post_id ) )
            return;
    } else {
        if ( !current_user_can( 'edit_post', $post_id ) )
            return;
    }
    $ranch_location = $_POST['ranch_location'];
    update_post_meta( $post_id, 'ranch_location', $ranch_location );

}

Any and all help is so appreciated. Thanks!

UPDATE ON SOME TROUBLESHOOTING:

So I'm playing with the save function some to troubleshoot. So I removed all the security verification in a variety of ways. First I left just the 'nonce' in the save function...

/*Custom META Boxes for Ranch Custom Post Types*/
/*Ranch Details META Box*/
add_action( 'add_meta_boxes', 'ranch_location_meta' );
function ranch_location_meta() {
add_meta_box(
'ranch_location_meta',
__( 'Ranch Location', 'myplugin_textdomain' ),
'ranch_location_meta_content',
'ranch',
'side',
'high'
);
}

function ranch_location_meta_content( $post ) {
wp_nonce_field( plugin_basename( __FILE__ ), 'ranch_location_meta_content_nonce' );
echo '<label for="ranch_location"></label>';
echo '<input type="text" id="ranch_location" name="ranch_location" placeholder="City &     State" />';
}

function ranch_location_save( $post_id ) {

if ( !wp_verify_nonce( $_POST['ranch_location_meta_content_nonce'], plugin_basename(     __FILE__ ) ) )
return;


$ranch_location = $_POST['ranch_location'];
update_post_meta( $post_id, 'ranch_location', $ranch_location );
}
add_action( 'save_post', 'ranch_location_save' );`

The save function still failed. No data remained in the meta box.

Then I removed the nonce all together, leaving this in the save function...

function ranch_location_save( $post_id ) {

if ( !wp_verify_nonce( $_POST['ranch_location_meta_content_nonce'], plugin_basename(     __FILE__ ) ) )
return;


$ranch_location = $_POST['ranch_location'];
update_post_meta( $post_id, 'ranch_location', $ranch_location );
}
add_action( 'save_post', 'ranch_location_save' );`

Still nothing being saved. SOOOOOO...does this mean that my issue has something to do with how the update_post_meta is finding the data to populate? Because the rest of the other stuff is just security, right? Without it all should just populate no problem, right? Excuse me if I'm wrong here. I'm a bit of a PHP hack. ;)

  • Is the line with $_POST['ranch_location'] executing? If yes, does it contain the correct data? – mor7ifer Feb 4 '15 at 21:24
  • Have you tried error logging at each stage? Please add the log output to your question, it will help pinpoint the issue. – shahar Feb 5 '15 at 7:21
1

Have you noted that you are verifing that the post type is a page and that user can edit pages and posts?

if ( 'page' == $_POST['post_type'] ) {
    if ( !current_user_can( 'edit_page', $post_id ) )
        return;
} else {
    if ( !current_user_can( 'edit_post', $post_id ) )
        return;
}

It should be:

if ( 'ranch' == $_POST['post_type'] ) {
    if ( !current_user_can( 'edit_post', $post_id ) )
        return;
} else {
    if ( !current_user_can( 'edit_post', $post_id ) )
        return;
}

Note: by default standard post capabilities are assigned to custom post types. If you have changed this, you must adjust current_user_can( 'edit_post', $post_id ) width correct capability.

Plus, you may want to fill the meta box input with previous value:

function ranch_location_meta_content( $post ) {

    $previous_value = get_post_meta( $post->ID, 'ranch_location', true );
    wp_nonce_field( plugin_basename( __FILE__ ), 'ranch_location_meta_content_nonce' );
    echo '<label for="ranch_location"></label>';
    echo '<input value="'.$previous_value.'" type="text" id="ranch_location" name="ranch_location"     placeholder="Enter  Ranch Location Here" />';

}
  • Thanks for the response. Although that change didn't solve the issue. My META box still won't save the data in my custom post. Regarding your note, I'm not exactly sure how I approach that. I don't think I changed anything in terms of default capabilities. I am using Foundation 5 to bootstrap the project. Could the framework have changed those capabilities? – Robert Rhu Feb 4 '15 at 18:20
  • Bootstrap and Foundation are frontend frameworks, nothing to do with WordPress. Why do you say it is not saved? How are you checking it is not saved? – cybmeta Feb 5 '15 at 8:06
  • I think that you are saying that the field is not saved because you always see the form field empty; that doesn't mean that it has not been saved. I've updated the answer with code that fill the form field with previous saved value. – cybmeta Feb 5 '15 at 8:17
0

Try basename( __FILE__ ) instead of plugin_basename( __FILE__ )

I think your nonce is not verifying.

  • Thanks for the response. No love with that change though. – Robert Rhu Feb 5 '15 at 1:36
0

It looks like what might be happening is that it is in fact saving it, but you are just not outputting the saved value. For example if you have a text input you will need to get the meta first and then echo it as the value attribute:

<?php
 $my_meta = get_post_meta( $post->ID, 'meta_key', true );
?>

<input type="text" name="name" id="id" value="<?php echo esc_attr($my_meta); ?>" />

This is just a very basic example to show what I mean.

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.