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. ;)
$_POST['ranch_location']
executing? If yes, does it contain the correct data?