Wordpress 如何设置自定义元框中的单选按钮?
我创建了一个自定义元框,您可以在其中从一些单选按钮中选择一个值,并将其保存到wordpress数据库中的post_元表中。使用以下代码保存值:Wordpress 如何设置自定义元框中的单选按钮?,wordpress,radio-button,checked,meta-boxes,Wordpress,Radio Button,Checked,Meta Boxes,我创建了一个自定义元框,您可以在其中从一些单选按钮中选择一个值,并将其保存到wordpress数据库中的post_元表中。使用以下代码保存值: function save_value_of_my_custom_metabox ($post_id, $post){ $post_id = get_the_ID(); $new_meta_value = ( isset( $_POST['the_name_of_the_radio_buttons'] ) ? sanitize_html_
function save_value_of_my_custom_metabox ($post_id, $post){
$post_id = get_the_ID();
$new_meta_value = ( isset( $_POST['the_name_of_the_radio_buttons'] ) ? sanitize_html_class( $_POST['the_name_of_the_radio_buttons'] ) : '' );
$meta_key = 'my_key';
update_post_meta( $post_id, $meta_key, $new_meta_value );
}
但是如果文章将被再次编辑,我希望当前值设置为选中的单选按钮。最好的方法是什么?以下是显示元框的功能:
function my_custom_meta_box( $object, $box ) {
$post_id=get_the_ID();
$key='my_key';
$the_value_that_should_be_set_to_checked=get_post_meta( $post_id, $key);
//$the_value_that_should_be_set_to_checked[0] returns the value as string
?>
<label for="my_custom_metabox"><?php _e( "Choose value:", 'choose_value' ); ?></label>
<br />
<input type="radio" name="the_name_of_the_radio_buttons" value="value1">Value1<br>
<input type="radio" name="the_name_of_the_radio_buttons" value="value2">Value2<br>
<input type="radio" name="the_name_of_the_radio_buttons" value="value3">Value3<br>
<input type="radio" name="the_name_of_the_radio_buttons" value="value4">Value4<br>
<?php
}
我可以写一些东西,比如ifasset$the_value_,它应该被设置为_checked[0]==该行的值echo checked='checked';每行都有,但我觉得不太优雅。在wordpress中使用javascript也相当复杂,因为我必须使用钩子,将脚本排队,仅仅为了用一行javascript更改checked属性,这是不值得的。这方面的最佳做法是什么?我找到了一个可行的解决方案,但我认为你不应该这样做。仍在寻求更好的解决方案 此代码是在上面的php代码下添加的:
if(isset($$the_value_that_should_be_set_to_checked[0])){
$the_value_that_should_be_set_to_checked= $the_value_that_should_be_set_to_checked[0];
}
else{
$the_value_that_should_be_set_to_checked='';
}
下面是我在单选按钮下面添加的代码:
<script type="text/javascript">
jQuery(document).ready(function () {
var checked_value= <?php echo json_encode($the_value_that_should_be_set_to_checked);?>;
if(checked_value!==''){
jQuery("input[name=the_name_of_the_radio_buttons][value="+checked_value+"]").attr('checked', 'checked');
}
});
</script>
备注:$selector将不起作用,但这可能取决于您使用的主题。我假设您正在尝试为“Posts”添加自定义元框。下面的代码将适用于您。它将在添加新帖子或编辑帖子屏幕上显示单选按钮。请阅读代码中的注释。它将帮助您理解代码 您可以使用WordPress的checked功能来决定是否选择单选按钮 如果您有任何疑问,请随时询问
/**
* Adds a box to the main column on the Post add/edit screens.
*/
function wdm_add_meta_box() {
add_meta_box(
'wdm_sectionid', 'Radio Buttons Meta Box', 'wdm_meta_box_callback', 'post'
); //you can change the 4th paramter i.e. post to custom post type name, if you want it for something else
}
add_action( 'add_meta_boxes', 'wdm_add_meta_box' );
/**
* Prints the box content.
*
* @param WP_Post $post The object for the current post/page.
*/
function wdm_meta_box_callback( $post ) {
// Add an nonce field so we can check for it later.
wp_nonce_field( 'wdm_meta_box', 'wdm_meta_box_nonce' );
/*
* Use get_post_meta() to retrieve an existing value
* from the database and use the value for the form.
*/
$value = get_post_meta( $post->ID, 'my_key', true ); //my_key is a meta_key. Change it to whatever you want
?>
<label for="wdm_new_field"><?php _e( "Choose value:", 'choose_value' ); ?></label>
<br />
<input type="radio" name="the_name_of_the_radio_buttons" value="value1" <?php checked( $value, 'value1' ); ?> >Value1<br>
<input type="radio" name="the_name_of_the_radio_buttons" value="value2" <?php checked( $value, 'value2' ); ?> >Value2<br>
<input type="radio" name="the_name_of_the_radio_buttons" value="value3" <?php checked( $value, 'value3' ); ?> >Value3<br>
<input type="radio" name="the_name_of_the_radio_buttons" value="value4" <?php checked( $value, 'value4' ); ?> >Value4<br>
<?php
}
/**
* When the post is saved, saves our custom data.
*
* @param int $post_id The ID of the post being saved.
*/
function wdm_save_meta_box_data( $post_id ) {
/*
* We need to verify this came from our screen and with proper authorization,
* because the save_post action can be triggered at other times.
*/
// Check if our nonce is set.
if ( !isset( $_POST['wdm_meta_box_nonce'] ) ) {
return;
}
// Verify that the nonce is valid.
if ( !wp_verify_nonce( $_POST['wdm_meta_box_nonce'], 'wdm_meta_box' ) ) {
return;
}
// If this is an autosave, our form has not been submitted, so we don't want to do anything.
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return;
}
// Check the user's permissions.
if ( !current_user_can( 'edit_post', $post_id ) ) {
return;
}
// Sanitize user input.
$new_meta_value = ( isset( $_POST['the_name_of_the_radio_buttons'] ) ? sanitize_html_class( $_POST['the_name_of_the_radio_buttons'] ) : '' );
// Update the meta field in the database.
update_post_meta( $post_id, 'my_key', $new_meta_value );
}
add_action( 'save_post', 'wdm_save_meta_box_data' );
提供您使用过的动作挂钩。添加动作“loadpost.php”、“我的自定义元框设置”;添加“loadpostnew.php”、“my_custom_meta_box_setup”动作;在设置功能中:添加动作“添加元框”、“我的自定义元框”;并添加"保存"操作","保存我的"自定义"元数据库的"值",10,2 ;;非常感谢你!我希望避免在每一行都检查它,但我想那是不可能的。但我使用WP的动态添加行选择,因此它是伟大的。感谢您对wp nonce安全问题的评论。我仍然需要整合这一点。