我正在创建一个wordpress插件,我想将一个单选按钮值保存到wordpress数据库中,以便以后在另一个函数中使用它。但我不知道怎么做。我尝试了会话,但会话过期时此值将丢失。你能告诉我怎么做吗?
这是我的代码:

function e_option_page() { ?>
    <form action="" id="testimonialsform" method="post">
        <input type="radio" name="Option" value="Option 1">Option 1
        <input type="radio" name="Option" value="Option 2">Option 2
        <input type="radio" name="Option" value="Option 3">Option 3
        <input type="submit" name="submit" value="Submit" />
    </form>
    <?php
    if (isset($_POST['submit'])) {
        if(isset($_POST['Option'])){
            echo "You have selected :".$_POST['Option'];  //  Displaying Selected Value
        }//End isset
    }//End isset
}//End function


function e_setting_page() {
    add_submenu_page('edit.php?post_type=testimonials', 'Settings', 'Settings', 'edit_posts', "settings",'e_option_page');
}

add_action('admin_menu' , 'e_setting_page');

最佳答案

您可以将任何选项值保存在“wp_usermeta”表中。

if (isset($_POST['submit'])) {
    if(isset($_POST['Option'])){
        //echo "You have selected :".$_POST['Option'];  //  Displaying Selected Value
        // Save data
        global $wpdb;
        $table = $wpdb->prefix.'usermeta';
        $data = array('meta_key' => 'your_option_name', 'meta_value' => $_POST['Option']);
        $format = array('%s','%s');
        $wpdb->insert($table,$data,$format);
        $my_id = $wpdb->insert_id;
        print_r($my_id);
    }//End isset
}//End isset

之后,可以使用此选项选择值。
现在您可以检查选项值。
// So check and make sure the stored value matches $new_value.
if ( $new_value = get_user_meta( $user_id=0,  'your_option_name', true ) ) {
    echo $new_value;
}else{
    wp_die( __( 'An error occurred', 'textdomain' ) );
}

也可以更新元密钥
// Will return false if the previous value is the same as $new_value.
$updated = update_user_meta( $user_id=0, 'your_option_name', 'new_value' );

08-19 09:19