$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'); ?>get_theme_mod doesn't return the theme customizer preview's new values in after_setup_theme hook|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)

get_theme_mod doesn't return the theme customizer preview's new values in after_setup_theme hook

matteradmin10PV0评论

If I print the value of get_theme_mod( 'enable_sleek_header', false ) it is always the previously saved value. If I print the same thing in the header of the theme, it returns the value from customizer.

  • Is it the expected behaviour?
  • Am I using the wrong hook?

Thanks


<?php

add_action( 'after_setup_theme', 'pagespeed_register_menus' );


    function pagespeed_register_menus() {

        //Not getting the modified theme_mod from the customizer without saving.
        register_nav_menus( array(
            'secondary' => __( 'Navigation above header', 'page-speed' ),
        ) );

        if ( get_theme_mod( 'enable_sleek_header', false ) ) {
            register_nav_menus( array(
                'header' => __( 'Navigation menu in header', 'page-speed' ),
            ) );
        } else {
            register_nav_menus( array(
                'primary' => __( 'Navigation below header', 'page-speed' ),
            ) );
        }
        register_nav_menus( array(
            'footer_links' => __( 'Footer links', 'page-speed' ),
        ) );

    }

If I print the value of get_theme_mod( 'enable_sleek_header', false ) it is always the previously saved value. If I print the same thing in the header of the theme, it returns the value from customizer.

  • Is it the expected behaviour?
  • Am I using the wrong hook?

Thanks


<?php

add_action( 'after_setup_theme', 'pagespeed_register_menus' );


    function pagespeed_register_menus() {

        //Not getting the modified theme_mod from the customizer without saving.
        register_nav_menus( array(
            'secondary' => __( 'Navigation above header', 'page-speed' ),
        ) );

        if ( get_theme_mod( 'enable_sleek_header', false ) ) {
            register_nav_menus( array(
                'header' => __( 'Navigation menu in header', 'page-speed' ),
            ) );
        } else {
            register_nav_menus( array(
                'primary' => __( 'Navigation below header', 'page-speed' ),
            ) );
        }
        register_nav_menus( array(
            'footer_links' => __( 'Footer links', 'page-speed' ),
        ) );

    }
Share Improve this question asked Jan 1, 2018 at 18:12 Satish GandhamSatish Gandham 4013 silver badges5 bronze badges 1
  • I am sure that after_setup_theme is the wrong approach , maybe this helps codex.wordpress/Theme_Customization_API – seot Commented Aug 30, 2018 at 19:48
Add a comment  | 

1 Answer 1

Reset to default 0

To register my menus I usually use the init action hook, maybe you could try it.

add_action( 'init', 'pagespeed_register_menus' );

instead of

add_action( 'after_setup_theme', 'pagespeed_register_menus' );
Post a comment

comment list (0)

  1. No comments so far