• Resolved nicholasrees

    (@nicholasrees)


    I’m pulling my hair out over this one. I’m adding customizer functionality to allow my girlfriend style a website that I’m building. She wants to be able to change the background color of the menu, and given that I’ve done this with several other divs on the theme, I figured this would be no big issue; however, she uses chrome, and I use firefox.

    My customizer options for modifying the style of the background on menu-items works just fine, and the style tag I’m using to grab the theme_mod also works just fine on pageload–in both firefox and in chrome. The preview works just fine in firefox as well, selective refresh and everything. However, when the customizer is being used on chrome, the the selective refresh correctly grabs the style tag, and then completely breaks EVERY SINGLE style element for everything else that the customizer has customized.

    If you modify another control, the settings revert, but modify a control related to one of the menu-items, and it breaks everything again (and by everything, I mean everything that is an style tag, and not anything in a <link> .css file like style.css for example).

    Because looping through all menu items and setting all that up (for both the customizer and the inline style tag) makes the code fairly verbose, I’ve hardcoded one single menu-item into my code such that I can provide you guys with an example:

    
    $wp_customize->add_setting( 'menu-item-8_bg_color', array(
            'type' => 'theme_mod',
            'transport' => 'postMessage',
            'sanitize_callback' => 'sanitize_hex_color'
            )
        );
    
        $wp_customize->add_control( new WP_Customize_Color_Control(
            $wp_customize,
            'menu-item-8_bg_color',
            array(
                'section' => 'menu',
                'setting' => 'menu-item-8_bg_color',
                'label' => 'home background color',
                'description' => 'shows if no background image or if image has transparency.',
                'setting' => 'menu-item-8_bg_color'
                )
            )
        );
    
        $wp_customize->selective_refresh->add_partial( 'menu-item-8_bg_color', array(
            'selector' => '#menu-item-styles',
            'container_inclusive' => True,
            'render_callback' => function() { return menu_items_style(); },
            'fallback_refresh' => True
            )
        );
    
    function menu_items_style() {
    
    return '<style id="menu-item-styles" type="text/css">
        #menu-item-8 {
        background:          <?= get_theme_mod( 'menu-item-8_bg_color' ) ?>;
        }
    </style>';
    
    }

    I have tried escaping out of php, echoing echoing, and (as above) simply returning out the style tag of menu_items_style().
    Also remember, this is a simplified version of what my code is since it requires me to loop through menus and all of that junk. I have simplified this to hardcode the value of a menu-item (which, can obviously change if you add/delete menu-items from a menu) only for the purposes of debugging. I don’t think the rest of that code has anything to do with this because I’m experiencing exactly the same thing in chrome (but no problem in firefox).
    Another point to mention, unless I’m very foolish, I’m really confident that the style tag is correct here, because–in chrome–if I simply edit the style element (in the chrome inspector), change nothing, and then exit editing the style element, then Chrome starts displaying all of the styles correctly again. I can even edit a different style element and chrome then updates accordingly.
    Again, this is ONLY happening in Chrome, and ONLY with the menu-item control(s). All other controls work just fine in the customizer and even in chrome. I’ve even hardcoded in the background css attribute to something like red, and it still breaks all styles.

    There are no errors from the javascript console and no errors from php debugging or in debug.log (both of which are obviously turned on). And chrome is definitely getting the updated inline style tag in the preview window, it just breaks everything.

    I have asked about this on IRC and was recommended to make the post here. My gut tells me that this is a chrome problem (since it works on firefox), but its really driving me up the wall as to why other customize controls work just fine on chrome (that modify background color like this as well).

    I simply just want to hear if anyone else can replicate the problem to know if this is my own stupidity or if this is a problem with chrome.

    Thanks for your time.

    • This topic was modified 5 years, 9 months ago by nicholasrees.
Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter nicholasrees

    (@nicholasrees)

    I forgot to add the code that actually defines the section where the menu-item controls sit:

    $wp_customize->add_section( 'content', array(
    'title' => 'Customize the content appearance',
    'description' => 'Customize the content appearance here.'
    )
    );

    Sorry for making such a long post, I don’t know how else to succinctly explain the problem.

    Perhaps there is a syntax error, and Chrome is not as forgiving as Firefox.
    There are several things which seem questionable, but the biggest one is using the partial. Why go to PHP to generate CSS when you can simply use JS and change the style?

    I’ve found that the easiest way to do colors is with CSS variables. You get instant feedback and the styles are so easy to output for the front end.

    Other things that don’t quite line up:
    the section name is ‘menu’ or ‘content’ ?
    the sanitize callback (does it include the # or not?)
    the True value should be true ?
    the render callback is an anonymous function that just calls a function (why?)
    the background shorthand resets all properties of the background

    You can read the code of my theme https://wordpress.org/themes/twenty8teen, which uses CSS variables for the colors (and other things), https://themes.trac.wordpress.org/browser/twenty8teen/20200614/style.css#L334
    JS to show it in the Customizer, https://themes.trac.wordpress.org/browser/twenty8teen/20200614/js/customizer-preview.js#L29
    the PHP function to generate the CSS for the front end, https://themes.trac.wordpress.org/browser/twenty8teen/20200614/functions.php#L464
    and I use a partial for the header image and blog name.
    https://themes.trac.wordpress.org/browser/twenty8teen/20200614/inc/theme-customizer.php#L468

    Thread Starter nicholasrees

    (@nicholasrees)

    I’m using PHP to generate the css because partials under wp_customize_manager you can simply throw in a function to the manager instead of having to enqueue js. Additionally you can write the entire customizer with only php, which seems obviously advantageous to me. I don’t quite understand the “why” question. Asking why I’m trying to do something doesn’t actually solve the issue I’m having.

    You’re right that I copied the incorrect section, there indeed is a section defined ‘menu’ as seen in the control parameters. If this were my problem, then I wouldn’t be able to see the control, but that is not the problem I described. The problem I described is chrome breaking the style tags–for not only this tag, but all tags on the site.

    The sanitize callback is for hex codes. I’m also using wp_customize_color_control, so yes the sanitize callback and the color control is supposed to be used together in this way. All my other controls use these functions together, and there is no problem there. Also, I can verify that the correct values for the background are being added into the style tag–only that chrome will not display them for some reason.

    With respect to the boolean values, from PHP documentation on boolean values:
    “To specify a boolean literal, use the constants TRUE or FALSE. Both are case-insensitive.”

    With respect to the anonymous function, I had trouble simply just passing in the function name to the render_callback. I don’t know what the issue was there, and in either case the wordpress theme handbook does essentially the same thing (albeit, passing a parameter into the function). I don’t see why modifying this would change my functionality.

    And yes, that’s exactly what I want to do by the way. Take any background property already defined for #menu-item-8 and replace it with the value from get_theme_mod( ‘menu-item-8’ ).

    Maybe you could check and see what is invalid about the style tag?

    function menu_items_style() {
    
    return '<style id="menu-item-styles" type"text/css">
        #menu-item-8 {
            background:    #ff000;
    }
    </style>'
    }

    Also breaks styles when the partial is rendered. Everything loads into the html of the partial, and that exact style is inserted and still breaks everything. It doesn’t make sense.

    • This reply was modified 5 years, 9 months ago by nicholasrees.
    • This reply was modified 5 years, 9 months ago by nicholasrees.
    • This reply was modified 5 years, 9 months ago by nicholasrees.
    • This reply was modified 5 years, 9 months ago by nicholasrees.
Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Customizer preview styles breaks on chrome’ is closed to new replies.