Как проверить переключатели в API настроек WordPress? - PullRequest
0 голосов
/ 27 ноября 2011

У меня есть тема, которую я создаю, которая позволяет пользователям менять стиль, нажимая переключатель.Я получил функциональность для работы без обратного вызова проверки, но с тех пор добавили текстовые поля на странице параметров, которые требуют проверки.Но теперь переключатели изменения стиля не работают с валидацией (если я не включил код валидации).Похоже, что 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;
    }

Я пробовал это с переключателями, ноэто не работает.

1 Ответ

0 голосов
/ 29 ноября 2011

Я нашел способ включения белых кнопок для проверки, даже если я не уверен, действительно ли они были проверены.Я установил для всей группы переключателей значение NULL, например:

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

Если у кого-то есть лучший способ сделать это, я открыт для предложений.

...