我需要获取控制选项以清除回调

时间:2019-01-10 作者:Schwarttzy

Code

目前,我正在进行一个项目,用简单数组自动生成所有主题定制器选项。

\'nav_font_style\' => array(
    \'default_options\'  => array(
        1                   => \'default\',),
    \'css\'               => "    nav#for-mobile h1 { font-family: $",
    \'stylesheet_handle\' => \'semperfi-navigation\',
    \'label\'             => __(\'Font\', \'semper-fi-lite\'),
    \'description\'       => array(
        1                   => \'\', ),
    \'panel_title\'       => __(\'Navigation\', \'semper-fi-lite\'),
    \'panel_priority\'    => 1,
    \'priority\'          => 10,
    \'section_title\'     => __(\'Menu Title\', \'semper-fi-lite\'),
    \'section_priority\'  => 10,
    \'selector\'          => \'nav#for-mobile h1\',
    \'type\'              => \'font\'),
此数组具有生成主题选项所需的一切。Bellow是它将通过的循环。

            // Add a font selector to Customizer
        if ($values[\'type\'] == \'font\') {

            $wp_customize->add_setting( $option . \'_\' . $i, array(
                \'default\'           => \'Default\',
                \'sanitize_callback\' => \'semperfi_sanitize_css\', ) );

            $wp_customize->add_control( $option . \'_\' . $i . \'_control\', array(
                \'section\'           => str_replace( "~", $semperfi_customizer_multi_dimensional_array[$i], $section_title_transformed ),
                \'label\'             => $values[\'label\'],
                \'description\'       => $values[\'description\'][$i],
                \'priority\'          => $values[\'priority\'],
                \'type\'              => \'select\',
                \'settings\'          => $option . \'_\' . $i,
                \'stylesheet_handle\' => $values[\'stylesheet_handle\'],
                \'choices\'           => $finalized_google_font_array));

        }
当我转到sanitize\\u回调时,我在使用它访问所有选项时遇到了问题,就像上面解释的那样How to get input_attrs in the sanitize function?

function semperfi_sanitize_css( $input , $setting ) {

set_theme_mod( \'semperfi_testing\' , $setting->manager->get_control( \'nav_font_style_1\' )->input_attrs );

return $input;
}

上面的清理功能只是为了测试,但我确实需要访问句柄,以便将CSS应用于正确的工作表样式。

谢谢你的帮助!

2 个回复
SO网友:Joy Reynolds

你不需要输入属性,你需要选择。我就是这么做的:

function mytheme_sanitize_choice( $input, $setting ) {
    $control = $setting->manager->get_control( $setting->id );
    $valid = $control->choices;
    return array_key_exists( $input, $valid ) ? $input : $setting->default;
}

SO网友:Schwarttzy

以下是解决方案。。。我想我发布后会得到它。

// Add a font selector to Customizer
            if ($values[\'type\'] == \'font\') {

                $wp_customize->add_setting( $option . \'_\' . $i, array(
                    \'default\'           => \'Default\',
                    \'sanitize_callback\' => \'semperfi_sanitize_css\', ) );

                $wp_customize->add_control( $option . \'_\' . $i , array(
                    \'choices\'           => $finalized_google_font_array,
                    \'description\'       => $values[\'description\'][$i],
                    \'input_attrs\'       => array(
                            \'stylesheet_handle\' => $values[ \'input_attrs\' ][ \'stylesheet_handle\' ],
                            \'css\'               => $values[ \'input_attrs\' ][ \'css\' ], ),
                    \'label\'             => $values[\'label\'],
                    \'priority\'          => $values[\'priority\'],
                    \'section\'           => str_replace( "~", $semperfi_customizer_multi_dimensional_array[$i], $section_title_transformed ),
                    \'settings\'          => $option . \'_\' . $i,
                    \'type\'              => \'select\', ) );

            }

        function semperfi_sanitize_css( $input , $setting ) {

        set_theme_mod( \'semperfi_testing\' , $setting->manager->get_control( $setting->id )->input_attrs[stylesheet_handle] );

        return $input;
        // just testing right now

    }

相关推荐

Need a help on sanitization

我在评论中有一个主题,我确实在像这样的少数情况下添加了“type”=>“select”部分。$wp_customize->add_setting( \'personal_lite_post_link\', array( \'default\' => \'enable\', \'sanitize_callback\' => \'personal_lite_select_callback\' ) ); $w