$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'); ?>Customizer Values Not Saving in Custom Theme|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)

Customizer Values Not Saving in Custom Theme

matteradmin9PV0评论

I'm adding a simple text field to the customizer in a custom theme using WP version 4.5.1. I frequently add these sorts of customizer additions to my themes, but this time no value is saving to the database.

I'm registering a custom section, field and control in the customizer, and calling it as a theme mod in the template file.

Code is as follows:

customizer.php

function mytheme_customize_register( $wp_customize ) {

    //Footer CTA
    $wp_customize->add_section(
        'footer',
        array(
            'title' => __('Footer CTA', 'mytheme'),
            'priority' => '200'
        )
    );

    $wp_customize->add_setting(
        'inspection_cta',
        array(
            'default' => __('Schedule Your Inspection', 'mytheme'),
            'type' => 'theme_mod',
            'sanitize_callback' => 'mytheme_sanitize_customizer_input'
        )
    );

    $wp_customize->add_control( new WP_Customize_Control(
            $wp_customize,
            'inspection_cta_text',
            array(
                'label' => __('Inspections CTA Text', 'mytheme'),
                'section' => 'footer',
                'settings' => 'inspection_cta',
                'type' => 'text',
                'priority' => '10'
            )
        )
    );

    $wp_customize->get_setting( 'inspection_cta_text' )->transport = 'postMessage';
}
add_action( 'customize_register', 'mytheme_customize_register' );

function mytheme_sanitize_customizer_input($input){
    return strip_tags(stripslashes($input));
}

The JavaScript for previewing:

wp.customize('inspection_cta_text', function(value){
        value.bind( function(to) {
            $('.footer-cta em').text(to);
        });
    });

And calling it in the theme file as such:

<h5 class="footer-cta"><em><?php echo get_theme_mod('inspection_cta_text'); ?></em></h5>

Everything registers correctly in the sense that the new section and option are available in the customizer, but live preview does not function and no values are saved to the database (wp_options > theme_mods_mytheme) on save.

Any input (including pointing out what is likely an obvious oversight on my part) is appreciated. Thanks!

I'm adding a simple text field to the customizer in a custom theme using WP version 4.5.1. I frequently add these sorts of customizer additions to my themes, but this time no value is saving to the database.

I'm registering a custom section, field and control in the customizer, and calling it as a theme mod in the template file.

Code is as follows:

customizer.php

function mytheme_customize_register( $wp_customize ) {

    //Footer CTA
    $wp_customize->add_section(
        'footer',
        array(
            'title' => __('Footer CTA', 'mytheme'),
            'priority' => '200'
        )
    );

    $wp_customize->add_setting(
        'inspection_cta',
        array(
            'default' => __('Schedule Your Inspection', 'mytheme'),
            'type' => 'theme_mod',
            'sanitize_callback' => 'mytheme_sanitize_customizer_input'
        )
    );

    $wp_customize->add_control( new WP_Customize_Control(
            $wp_customize,
            'inspection_cta_text',
            array(
                'label' => __('Inspections CTA Text', 'mytheme'),
                'section' => 'footer',
                'settings' => 'inspection_cta',
                'type' => 'text',
                'priority' => '10'
            )
        )
    );

    $wp_customize->get_setting( 'inspection_cta_text' )->transport = 'postMessage';
}
add_action( 'customize_register', 'mytheme_customize_register' );

function mytheme_sanitize_customizer_input($input){
    return strip_tags(stripslashes($input));
}

The JavaScript for previewing:

wp.customize('inspection_cta_text', function(value){
        value.bind( function(to) {
            $('.footer-cta em').text(to);
        });
    });

And calling it in the theme file as such:

<h5 class="footer-cta"><em><?php echo get_theme_mod('inspection_cta_text'); ?></em></h5>

Everything registers correctly in the sense that the new section and option are available in the customizer, but live preview does not function and no values are saved to the database (wp_options > theme_mods_mytheme) on save.

Any input (including pointing out what is likely an obvious oversight on my part) is appreciated. Thanks!

Share Improve this question asked Apr 27, 2016 at 18:09 DavidBrownDavidBrown 1231 silver badge13 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

This live preview code should be like this -

wp.customize('inspection_cta_text', function(value){
    value.bind( function(to) {
        $('.footer-cta').text(to);
    });
});

Basically you don't need the "em". And there always should be default value present in the theme mod. For example -

.footer {
     border-top: solid 1px #<?php echo get_theme_mod( 'background_color', '#fff' ); ?>;
}

In your case it would be -

<h5 class="footer-cta"><em><?php echo get_theme_mod('inspection_cta_text', 'default text'); ?></em></h5>

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far