Support » Themes and Templates » Sections added via $wp_customize->add_section() disappear

  • Resolved JB Christy

    (@jbchristy)


    I developed a custom theme using WordPress 3.9. I relied heavily on the WP_Customize_Manager API to add new sections, settings and controls, all triggered by the customize_register action. Recently my site was automatically upgraded to 4.1.2. Now when I go to the Appearance > Customize page, I see the sections I added with $wp_customize->add_section() for a moment, but then they disappear from the left nav.

    Something has clearly changed between 3.9 and 4.1.2, but I don’t know what and I don’t know how to fix it. Please advise on where I should start looking. The Appearance > Customize page is the only method my users have to set important options for the theme, and now they can’t access the settings they need.

    Thanks in advance.

Viewing 9 replies - 1 through 9 (of 9 total)
  • After further testing, I’ve learned that Appearance > Customize worked in 4.0.2 and failed in 4.1.2. I see that admin/js/customize-controls.js changed in 4.1.1, so I presume that’s where the bug was introduced.

    Moderator Jose Castaneda

    (@jcastaneda)

    THEME COFFEE MONKEY

    Have you tried deactivating all plugins to rule that out?

    Yes, I’m running on a development site and all plugins are deactivated. Somehow every section and every control I manually created is being perceived as not “active”.

    When the page loads, my sections initially display, but then the ‘ready’ event is triggered on the api.PreviewFrame, and its passed some ‘data’ that has elements activePanels, activeSections and activeControls. All of WordPress’s native panels, sections and controls are in that data structure and none of mine are. That data structure seems to be emitted by WP_Customize_Manager::customize_preview_settings(), but when I view source on the page the data structure doesn’t look at all like I think it should look based on that PHP code. And yet when I step into the Javascript code data has the expected structure.

    It looks like in 4.1 a new method was added to WP_Customize_Section, active(). Looking at the PHP code I’m guessing that I have to add a filter for customize_section_active to say the my sections are active. Maybe I have to do the same for all my controls as well. If that’s true, what a royal PITA it is.

    Upon further investigation, it appears hiding all my stuff by default was “an improvement in 4.0”. See the Contextual Controls section of the Customizer Improvements in 4.0 article. Apparently now, for every control I add, I need to pass an active_callback function that returns true when the user is previewing a “context” in which I want my control to be active. For some unfathomable reason, if I fail to provide that callback, say, because it never existed before, the default return value for active() is false. Meaning all my custom stuff disappears by default. Not exactly backwards compatible there. 🙁

    Actually, that didn’t do it. Still trying.

    Moderator Jose Castaneda

    (@jcastaneda)

    THEME COFFEE MONKEY

    Curious if you are using is_admin() on your code. I forgot that there was a ticket created for missing sections: https://core.trac.wordpress.org/ticket/30387

    The code in the function that adds all the sections and controls was not wrapped in is_admin(), but attaching the function to the customize_register action was, i.e.

    if ( is_admin() ) {
    add_action('customize_register', array($this, 'admin_theme_options'));
    }

    When I moved that add_action() call outside the if everything works again.

    THANK YOU!!! I would’ve never thought of that.

    Moderator Jose Castaneda

    (@jcastaneda)

    THEME COFFEE MONKEY

    You are most welcome! 🙂

    I’m glad you got it working!

    Jose Castaneda, you are the Shizzle!

    Thanks!

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Sections added via $wp_customize->add_section() disappear’ is closed to new replies.