$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>theme customizer - I need to get the control choices to sanitize_callback|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

theme customizer - I need to get the control choices to sanitize_callback

matteradmin8PV0评论

Code

Currently, I'm working project to automatically generate all my theme customizer options with simple array.

'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'),

This array has everything needed to generate a theme option. Bellow is the loop that it will run through.

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

        }

When I go the sanitize_callback I'm having issues using this to access all the choices like it's explained on 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;

}

The above sanitize function is just for testing but I really need to access the handle so that I can apply CSS to the correct sheet style.

Thanks for your help!

Code

Currently, I'm working project to automatically generate all my theme customizer options with simple array.

'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'),

This array has everything needed to generate a theme option. Bellow is the loop that it will run through.

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

        }

When I go the sanitize_callback I'm having issues using this to access all the choices like it's explained on 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;

}

The above sanitize function is just for testing but I really need to access the handle so that I can apply CSS to the correct sheet style.

Thanks for your help!

Share Improve this question asked Jan 10, 2019 at 18:31 SchwarttzySchwarttzy 13 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 0

You don't need the input_attrs, you need choices. This is what I do:

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

Here's the solution... figures I get it after I post it.

// 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

    }
Post a comment

comment list (0)

  1. No comments so far