如何在选中的自定义元框中设置单选按钮? [英] How to set radio buttons in custom meta box checked?

查看:54
本文介绍了如何在选中的自定义元框中设置单选按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个自定义元框,您可以在其中从一些单选按钮中选择一个值并将其保存到 wordpress 数据库中的 post_meta 表中.使用以下代码保存值:

I created a custom meta box where you can choose a value from some radio buttons and save it to the post_meta table in the wordpress database. With the following code I save the value:

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 );
}

但是如果帖子将被再次编辑,我希望选中具有当前值的单选按钮.最好的方法是什么?这是显示元框的函数:

But if the post will be edited again I want the radio button with the current value to set checked. What is the best way to do that? Here is the function to display the meta box:

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
}

我可以在每一行中编写类似 if(isset($the_value_that_should_be_set_to_checked[0])=="value of that line") echo "checked='checked'"; 之类的东西,但那不会'对我来说似乎很优雅.在 wordpress 中使用 javascript 也非常复杂,因为我必须使用钩子,将脚本排入队列,仅仅为了用一行 javascript 更改已检查的属性,这是不值得的.最佳做法是什么?

I could write something like if(isset($the_value_that_should_be_set_to_checked[0])=="value of that line") echo "checked='checked'"; in every line but that doesn't seem very elegant to me. Using javascript is also pretty complicated in wordpress because I would have to use the hooks, enqueue the script and just for changing the checked property with one line of javascript it's not worth it. What's the best practice for that?

推荐答案

我假设您正在尝试为帖子"添加自定义元框.下面的代码对你有用.它将在添加新帖子或编辑帖子屏幕上显示单选按钮.请阅读代码中的注释.它将帮助您理解代码.

I am assuming that you are trying to add custom meta box for 'Posts'. Below code will work for you. It will show Radio buttons on add new post or edit post screen. Please read the comments in the code. It will help you in understanding the code.

您可以使用WordPress的checked功能来决定是否选中单选按钮.

You can use WordPress's checked function to decide whether to select the radio button or not.

如果您有任何疑问,请随时提问.

Feel free to ask if you have any doubts.

/**
 * 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' );

这篇关于如何在选中的自定义元框中设置单选按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆