如何验证 WordPress 设置 API 中的单选按钮?

发布于 2024-12-18 13:21:41 字数 6568 浏览 8 评论 0原文

我有一个正在组合的主题,允许用户通过单击单选按钮来更改样式。我的功能无需验证回调即可工作,但后来在需要验证的选项页面中添加了文本字段。但现在样式更改单选按钮不适用于验证(如果我不包含验证代码)。似乎设置 API 需要对所有内容进行验证。经过两天的谷歌搜索解决方案,我找不到任何答案。我试图找出如何验证单选按钮代码或跳过仅对单选按钮进行验证。这是相关的代码:

<?php
add_action('admin_init', 'mxs_admin_init');
add_action('admin_menu', 'mxs_admin_add_page');

function mxs_admin_add_page() {
    add_theme_page(
        'Mixin&apos; Styles Theme Options',
        'Mixin&apos; Styles Theme Options',
        'manage_options',
        'mixinstyles',
        'mxs_theme_options_page'
        );
    }

function mxs_admin_init() {
    register_setting(
        'mixinstyles_theme_options',
        'mixinstyles_theme_options',
        'mixinstyles_options_validate'
        );

    add_settings_section( // Styles section
        'mixinstyles_main',
        'Mixin&apos; Styles Style Settings',
        'mxs_theming_section_text',
        'mixinstyles'
        );
    add_settings_field(
        'custom_style_buttons',
        '<strong>Color Schemes</strong>',
        'mxs_custom_style_buttons',
        'mixinstyles',
        'mixinstyles_main'
        );
    ...
function mxs_custom_style_buttons() { 
    $options = get_option('mixinstyles_theme_options');
    //var_dump($options); //for debugging
        echo "<div class='radiobutton-wrap'> \n";
        echo "<div class='radiobutton-padding'> \n <input type='radio' id='default_style' value='default_style' name='mixinstyles_theme_options[custom_style_buttons]'" . checked( $options['custom_style_buttons'], 1 ) . " /><img src='" . get_bloginfo('template_directory') . "/images/default_screenshot.png' alt='Default style' /><br /><label for='default_style'>Default Style</label> </div> \n";
        echo "<div class='radiobutton-padding'> \n <input type='radio' id='blue_orange' value='blue_orange' name='mixinstyles_theme_options[custom_style_buttons]' /><img src='" . get_bloginfo('template_directory') . "/images/blueorange_screenshot.png' alt='Blue/Orange style' /><br /><label for='blue_orange'>Blue/Orange</label> </div> \n";
        echo "<div class='radiobutton-padding'> \n <input type='radio' id='violet_yellow' value='violet_yellow' name='mixinstyles_theme_options[custom_style_buttons]' /><img src='" . get_bloginfo('template_directory') . "/images/violetyellow_screenshot.png' alt='Violet/Yellow style' /><br /><label for='violet_yellow'>Violet/Yellow</label> </div> \n";
        echo "</div> \n";
        echo "<div class='radiobutton-wrap'> \n";
        echo "<div class='radiobutton-padding'> \n <input type='radio' id='magenta_green' value='magenta_green' name='mixinstyles_theme_options[custom_style_buttons]' /><img src='" . get_bloginfo('template_directory') . "/images/magentagreen_screenshot.png' alt='Magenta/Green style' /><br /><label for='magenta_green'>Magenta/Green</label></div> \n";
        echo "<div class='radiobutton-padding'> \n <input type='radio' id='orange_blue' value='orange_blue' name='mixinstyles_theme_options[custom_style_buttons]' /><img src='" . get_bloginfo('template_directory') . "/images/orangeblue_screenshot.png' alt='Orange/Blue style' /><br /><label for='orange_blue'>Orange/Blue</label></div> \n";
        echo "<div class='radiobutton-padding'> \n <input type='radio' id='yellow_violet' value='yellow_violet' name='mixinstyles_theme_options[custom_style_buttons]' /><img src='" . get_bloginfo('template_directory') . "/images/yellowviolet_screenshot.png' alt='Yellow/Violet style' /><br /><label for='yellow_violet'>Yellow/Violet</label></div> \n";
        echo "</div> \n";
    }
    ...

function mxs_style_switcher() {
    global $mixinstyles_theme_options;
    $options = get_option('mixinstyles_theme_options');

        switch ( $options['custom_style_buttons'] ) { //opens switch statement
            case "blue_orange":
                echo '<link rel="stylesheet" href="'; 
                bloginfo('template_directory');
                echo '/custom-styles/blue-orange.css" type="text/css" />' . "\n";
            break;
            case "violet_yellow":
                echo '<link rel="stylesheet" href="';
                bloginfo('template_directory');
                echo '/custom-styles/violet-yellow.css" type="text/css" />' . "\n";
            break;
            case "magenta_green":
                echo '<link rel="stylesheet" href="';
                bloginfo('template_directory');
                echo '/custom-styles/magenta-green.css" type="text/css" />' . "\n";
            break;
            case "orange_blue":
                echo '<link rel="stylesheet" href="';
                bloginfo('template_directory');
                echo '/custom-styles/orange-blue.css" type="text/css" />' . "\n";
            break;
            case "yellow_violet":
            echo '<link rel="stylesheet" href="';
            bloginfo('template_directory');
            echo '/custom-styles/yellow-violet.css" type="text/css" />' . "\n";
            break;
            default:
            echo '';
        } //closes switch statement
}
add_action('wp_head', 'mxs_style_switcher');
...

function mixinstyles_options_validate($input) { //opens mixinstyles_options_validate function
$options = get_option('mixinstyles_theme_options');

//for each radio button
//default
$options['default_style'] = $input['default_style'];
if ( !isset( $input['default_style'] ) ) {

    }

//blue orange
$options['blue_orange'] = $input['blue_orange'];
if ( !isset( $input['blue_orange'] ) ) {

    }

//violet yellow
$options['violet_yellow'] = $input['violet_yellow'];
if ( !isset( $input['violet_yellow'] ) ) {

    }

//magenta green
$options['magenta_green'] = $input['magenta_green'];
if ( !isset( $input['magenta_green'] ) ) {

    }

//orange blue
$options['orange_blue'] = $input['orange_blue'];
if ( !isset( $input['orange_blue'] ) ) {

    }

//yellow violet
$options['yellow_violet'] = $input['yellow_violet'];
if ( !isset( $input['yellow_violet'] ) ) {

    }
return $options;
}
?>

我在函数中对如下所示的复选框进行了类似的验证:

//check if checkbox has been checked
$options['remove_blogtitle'] = $input['remove_blogtitle'];
if ( !isset( $input['remove_blogtitle'] ) ) {
    $input['remove_blogtitle'] = null;
    }

我已经使用单选按钮尝试过此操作,但它不起作用。

I have a theme that I'm putting together that allows users to change a style by clicking a radio button. I got the functionality to work without a validation callback, but since have added text fields in the options page that require validation. But now the style changing radio buttons don't work with the validation (if I don't include validation code). It seems like the Settings API requires validation for everything. After googling for solutions for two days, I couldn't find any answer. I'm trying to find out how to either validate the radio button code or skip validation on just the radio buttons. Here is the associated code:

<?php
add_action('admin_init', 'mxs_admin_init');
add_action('admin_menu', 'mxs_admin_add_page');

function mxs_admin_add_page() {
    add_theme_page(
        'Mixin' Styles Theme Options',
        'Mixin' Styles Theme Options',
        'manage_options',
        'mixinstyles',
        'mxs_theme_options_page'
        );
    }

function mxs_admin_init() {
    register_setting(
        'mixinstyles_theme_options',
        'mixinstyles_theme_options',
        'mixinstyles_options_validate'
        );

    add_settings_section( // Styles section
        'mixinstyles_main',
        'Mixin' Styles Style Settings',
        'mxs_theming_section_text',
        'mixinstyles'
        );
    add_settings_field(
        'custom_style_buttons',
        '<strong>Color Schemes</strong>',
        'mxs_custom_style_buttons',
        'mixinstyles',
        'mixinstyles_main'
        );
    ...
function mxs_custom_style_buttons() { 
    $options = get_option('mixinstyles_theme_options');
    //var_dump($options); //for debugging
        echo "<div class='radiobutton-wrap'> \n";
        echo "<div class='radiobutton-padding'> \n <input type='radio' id='default_style' value='default_style' name='mixinstyles_theme_options[custom_style_buttons]'" . checked( $options['custom_style_buttons'], 1 ) . " /><img src='" . get_bloginfo('template_directory') . "/images/default_screenshot.png' alt='Default style' /><br /><label for='default_style'>Default Style</label> </div> \n";
        echo "<div class='radiobutton-padding'> \n <input type='radio' id='blue_orange' value='blue_orange' name='mixinstyles_theme_options[custom_style_buttons]' /><img src='" . get_bloginfo('template_directory') . "/images/blueorange_screenshot.png' alt='Blue/Orange style' /><br /><label for='blue_orange'>Blue/Orange</label> </div> \n";
        echo "<div class='radiobutton-padding'> \n <input type='radio' id='violet_yellow' value='violet_yellow' name='mixinstyles_theme_options[custom_style_buttons]' /><img src='" . get_bloginfo('template_directory') . "/images/violetyellow_screenshot.png' alt='Violet/Yellow style' /><br /><label for='violet_yellow'>Violet/Yellow</label> </div> \n";
        echo "</div> \n";
        echo "<div class='radiobutton-wrap'> \n";
        echo "<div class='radiobutton-padding'> \n <input type='radio' id='magenta_green' value='magenta_green' name='mixinstyles_theme_options[custom_style_buttons]' /><img src='" . get_bloginfo('template_directory') . "/images/magentagreen_screenshot.png' alt='Magenta/Green style' /><br /><label for='magenta_green'>Magenta/Green</label></div> \n";
        echo "<div class='radiobutton-padding'> \n <input type='radio' id='orange_blue' value='orange_blue' name='mixinstyles_theme_options[custom_style_buttons]' /><img src='" . get_bloginfo('template_directory') . "/images/orangeblue_screenshot.png' alt='Orange/Blue style' /><br /><label for='orange_blue'>Orange/Blue</label></div> \n";
        echo "<div class='radiobutton-padding'> \n <input type='radio' id='yellow_violet' value='yellow_violet' name='mixinstyles_theme_options[custom_style_buttons]' /><img src='" . get_bloginfo('template_directory') . "/images/yellowviolet_screenshot.png' alt='Yellow/Violet style' /><br /><label for='yellow_violet'>Yellow/Violet</label></div> \n";
        echo "</div> \n";
    }
    ...

function mxs_style_switcher() {
    global $mixinstyles_theme_options;
    $options = get_option('mixinstyles_theme_options');

        switch ( $options['custom_style_buttons'] ) { //opens switch statement
            case "blue_orange":
                echo '<link rel="stylesheet" href="'; 
                bloginfo('template_directory');
                echo '/custom-styles/blue-orange.css" type="text/css" />' . "\n";
            break;
            case "violet_yellow":
                echo '<link rel="stylesheet" href="';
                bloginfo('template_directory');
                echo '/custom-styles/violet-yellow.css" type="text/css" />' . "\n";
            break;
            case "magenta_green":
                echo '<link rel="stylesheet" href="';
                bloginfo('template_directory');
                echo '/custom-styles/magenta-green.css" type="text/css" />' . "\n";
            break;
            case "orange_blue":
                echo '<link rel="stylesheet" href="';
                bloginfo('template_directory');
                echo '/custom-styles/orange-blue.css" type="text/css" />' . "\n";
            break;
            case "yellow_violet":
            echo '<link rel="stylesheet" href="';
            bloginfo('template_directory');
            echo '/custom-styles/yellow-violet.css" type="text/css" />' . "\n";
            break;
            default:
            echo '';
        } //closes switch statement
}
add_action('wp_head', 'mxs_style_switcher');
...

function mixinstyles_options_validate($input) { //opens mixinstyles_options_validate function
$options = get_option('mixinstyles_theme_options');

//for each radio button
//default
$options['default_style'] = $input['default_style'];
if ( !isset( $input['default_style'] ) ) {

    }

//blue orange
$options['blue_orange'] = $input['blue_orange'];
if ( !isset( $input['blue_orange'] ) ) {

    }

//violet yellow
$options['violet_yellow'] = $input['violet_yellow'];
if ( !isset( $input['violet_yellow'] ) ) {

    }

//magenta green
$options['magenta_green'] = $input['magenta_green'];
if ( !isset( $input['magenta_green'] ) ) {

    }

//orange blue
$options['orange_blue'] = $input['orange_blue'];
if ( !isset( $input['orange_blue'] ) ) {

    }

//yellow violet
$options['yellow_violet'] = $input['yellow_violet'];
if ( !isset( $input['yellow_violet'] ) ) {

    }
return $options;
}
?>

I have a similar validation in the function for a check box written like this:

//check if checkbox has been checked
$options['remove_blogtitle'] = $input['remove_blogtitle'];
if ( !isset( $input['remove_blogtitle'] ) ) {
    $input['remove_blogtitle'] = null;
    }

I've tried this with the radio buttons but it doesn't work.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

前事休说 2024-12-25 13:21:42

我找到了一种将单选按钮列入白名单以进行验证的方法,即使我不确定单选按钮是否确实经过验证。我将整组单选按钮设置为空,如下所示:

$options['custom_style_buttons'] = $input['custom_style_buttons'];
if ( !isset( $input['custom_style_buttons'] ) ) {
$input['custom_style_buttons'] = null;
}

如果有人有更好的方法来做到这一点,我愿意接受建议。

I found a way to whitelist radio buttons for validation, even though I'm not sure if the radio buttons were actually validated. I set the whole group of radio buttons to null, like this:

$options['custom_style_buttons'] = $input['custom_style_buttons'];
if ( !isset( $input['custom_style_buttons'] ) ) {
$input['custom_style_buttons'] = null;
}

If anybody has a better way to do this, I am open to suggestions.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文