Wordpress php 如何将 select 下拉列表的值添加和保存到自定义 post 类型
Wordpress php how to add and save the values of a select dropdown to custom post type
我在 wordpress 中创建了一个名为 "actors"
的自定义 post 类型
在这个自定义 post 类型中,我添加了一些元数据框,管理员可以在其中添加关于每个演员的自定义数据。
我需要这些元数据框之一作为 select 输入而不是文本输入,但是我不知道如何保存值并显示为 selected。
这是我当前的 php 代码:
// 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
}
}
我在这里明显遗漏了什么 - 我希望 "Gender" 选项是下拉菜单 select 输入
好的,我测试了你的代码,当你注释掉随机数时它会保存
if ( !isset($_POST['actorinfometa_noncename']) || !wp_verify_nonce( $_POST['actorinfometa_noncename'], basename(__FILE__) )) {
return $post->ID;
}
所以我猜你的随机数被破坏了。
在您的 actors_info()
中将其更改为
echo '<input type="hidden" name="actorinfometa_noncename" id="actorinfometa_noncename" value="' .
wp_create_nonce( 'actor_nonce' ) . '" />';
并将保存随机数更改为
if ( !isset($_POST['actorinfometa_noncename']) || !wp_verify_nonce( $_POST['actorinfometa_noncename'], 'actor_nonce' )) {
return $post->ID;
对我有用。希望这有帮助。
还要确保您有 save_post
操作。要么
add_action( 'save_post', array( $this, 'actor_info_save' ), 10, 2 );
或
add_action( 'save_post', 'Nial_Actors::actor_info_save', 10, 2 );
或者对于非面向对象的代码:
add_action( 'save_post', 'actor_info_save', 10, 2 );
我在 wordpress 中创建了一个名为 "actors"
的自定义 post 类型在这个自定义 post 类型中,我添加了一些元数据框,管理员可以在其中添加关于每个演员的自定义数据。
我需要这些元数据框之一作为 select 输入而不是文本输入,但是我不知道如何保存值并显示为 selected。
这是我当前的 php 代码:
// 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
}
}
我在这里明显遗漏了什么 - 我希望 "Gender" 选项是下拉菜单 select 输入
好的,我测试了你的代码,当你注释掉随机数时它会保存
if ( !isset($_POST['actorinfometa_noncename']) || !wp_verify_nonce( $_POST['actorinfometa_noncename'], basename(__FILE__) )) {
return $post->ID;
}
所以我猜你的随机数被破坏了。
在您的 actors_info()
中将其更改为
echo '<input type="hidden" name="actorinfometa_noncename" id="actorinfometa_noncename" value="' .
wp_create_nonce( 'actor_nonce' ) . '" />';
并将保存随机数更改为
if ( !isset($_POST['actorinfometa_noncename']) || !wp_verify_nonce( $_POST['actorinfometa_noncename'], 'actor_nonce' )) {
return $post->ID;
对我有用。希望这有帮助。
还要确保您有 save_post
操作。要么
add_action( 'save_post', array( $this, 'actor_info_save' ), 10, 2 );
或
add_action( 'save_post', 'Nial_Actors::actor_info_save', 10, 2 );
或者对于非面向对象的代码:
add_action( 'save_post', 'actor_info_save', 10, 2 );