Sam Skirrow Sam Skirrow - 5 months ago 8
PHP Question

Wordpress php how to add and save the values of a select dropdown to custom post type

I have created a custom post typ ewithin wordpress called "actors"

In this custom post type, I have added some metaboxes where the admin can add custom data about each actor.

I need one of these metaboxes to be a select input rather than a text input, however I can't figure out how to make the value save, and show as selected.

Here is my current php code:

// Add the Actors Meta Boxes
function add_actor_metaboxes() {
add_meta_box('actors_info', 'Actor Info', 'Nial_Actors::actors_info', 'actors', 'normal', 'default');
}

// The Actors Metabox
function actors_info() {
global $post;

// Noncename needed to verify where the data originated
echo '<input type="hidden" name="actorinfometa_noncename" id="actorinfometa_noncename" value="' .
wp_create_nonce( plugin_basename(__FILE__) ) . '" />';

// Get the location data if its already been entered
$spotlight_url = get_post_meta($post->ID, '_spotlight_url', true);
$actor_gender = get_post_meta($post->ID, '_actor_gender', true);
$actor_age = get_post_meta($post->ID, '_actor_age', true);
$actor_height = get_post_meta($post->ID, '_actor_height', true);
$actor_weight = get_post_meta($post->ID, '_actor_weight', true);

// Echo out the field
echo '<p>Spotlight URL</p>';
echo '<input type="text" name="_spotlight_url" value="' . $spotlight_url . '" class="widefat" />';
echo '<p>Gender</p>';

echo '<select name="_actor_gender" id="actor_gender">';
echo '<option value="null" ' . selected( $actor_gender, 'null' ) . '>--</option>';
echo '<option value="male" ' . selected( $actor_gender, 'male' ) . '>Male</option>';
echo '<option value="female" ' . selected( $actor_gender, 'female' ) . '>Female</option>';
echo '</select>';

echo '<p>Age</p>';
echo '<input type="text" name="_actor_age" value="' . $actor_age . '" class="widefat" />';
echo '<p>Height</p>';
echo '<input type="text" name="_actor_height" value="' . $actor_height . '" class="widefat" />';
echo '<p>Weight</p>';
echo '<input type="text" name="_actor_weight" value="' . $actor_weight . '" class="widefat" />';

}

// Save the Metabox Data
function actor_info_save($post_id, $post) {

// verify this came from the our screen and with proper authorization,
// because save_post can be triggered at other times
if ( !isset($_POST['actorinfometa_noncename']) || !wp_verify_nonce( $_POST['actorinfometa_noncename'], basename(__FILE__) )) {
return $post->ID;

}
//if ( !wp_verify_nonce( $_POST['actorinfometa_noncename'], plugin_basename(__FILE__) )) {
//return $post->ID;
//}

// Is the user allowed to edit the post or page?
if ( !current_user_can( 'edit_post', $post->ID ))
return $post->ID;

// OK, we're authenticated: we need to find and save the data
// We'll put it into an array to make it easier to loop though.

$actor_meta['_spotlight_url'] = $_POST['_spotlight_url'];
$actor_meta['_actor_gender'] = $_POST['_actor_gender'];
$actor_meta['_actor_age'] = $_POST['_actor_age'];
$actor_meta['_actor_height'] = $_POST['_actor_height'];
$actor_meta['_actor_weight'] = $_POST['_actor_weight'];

// Add values of $actor_meta as custom fields

foreach ($actor_meta as $key => $value) { // Cycle through the $actor_meta array!
if( $post->post_type == 'revision' ) return; // Don't store custom data twice
$value = implode(',', (array)$value); // If $value is an array, make it a CSV (unlikely)
if(get_post_meta($post->ID, $key, FALSE)) { // If the custom field already has a value
update_post_meta($post->ID, $key, $value);
} else { // If the custom field doesn't have a value
add_post_meta($post->ID, $key, $value);
}
if(!$value) delete_post_meta($post->ID, $key); // Delete if blank
}

}


Is there something obvious I am missing here - I want the "Gender" option to be a dropdown select input

Answer

Ok, I tested your code, and it saves when you comment out the nonce

if ( !isset($_POST['actorinfometa_noncename']) || !wp_verify_nonce( $_POST['actorinfometa_noncename'], basename(__FILE__) )) {
    return $post->ID;
}

So my guess is that your nonce is getting busted.

In your actors_info() change it to

echo '<input type="hidden" name="actorinfometa_noncename" id="actorinfometa_noncename" value="' .
wp_create_nonce( 'actor_nonce' ) . '" />';

And change save nonce to

if ( !isset($_POST['actorinfometa_noncename']) || !wp_verify_nonce( $_POST['actorinfometa_noncename'], 'actor_nonce' )) {
    return $post->ID;

Worked for me. Hope this helps.

Also make sure that you have save_post action. Either

add_action( 'save_post', array( $this, 'actor_info_save' ), 10, 2 );

Or

add_action( 'save_post', 'Nial_Actors::actor_info_save', 10, 2 );

Or for non object oriented code:

add_action( 'save_post', 'actor_info_save', 10, 2 );
Comments