Resetting the Customizer to a Blank Slate

Authored by

Sometimes it is desirable to be able to load the customizer without any of the panels, sections, controls, or settings that would normally be registered by core, themes, or plugins. For instance, in Customize Posts there is the ability to open the customizer via the “Preview” button from the edit post screen. When the customizer opens, only the section containing the controls for manipulating that specific post are shown shown in the customizer. I’ve seen this approach taken a few times, for example to create a specific view for managing only a plugin’s specific settings without anything else. Implementing this normally involves looking for some query param and then filtering out the loaded components and de-registering any sections and controls. When I’ve implemented this, I didn’t feel it was very clean and it seemed like there must be a better way. Today the question was raised again so I wanted to give it some more thought:

I believe I’ve come up with a very concise snippet that can serve as a starting point for any such plugins:

add_filter( 'customize_loaded_components', function() {

	/*
	 * Note the customize_register action is triggered in
	 * WP_Customize_Manager::wp_loaded() which is itself the
	 * callback for the wp_loaded action at priority 10. So
	 * this wp_loaded action just has to be added at a
	 * priority less than 10.
	 */
	$priority = 1;
	add_action( 'wp_loaded', function() {

		/*
		 * Remove all constructs from being registered,
		 * whether in core, themes, or plugins.
		 */
		remove_all_actions( 'customize_register' );

		/*
		 * Now register your own customize_register
		 * callback which will register just the specific
		 * panels, sections, controls, settings, etc
		 * that are relevant. This can either be done
		 * at a location as follows or it can be done
		 * via a new wp_loaded handler at priority 9.
		 */
		// @todo add_action( 'customize_register', … );
	}, $priority );

	// Short-circuit widgets, nav-menus, etc from loading.
	$components = array();

	return $components;
} );

I’ve taken this snippet and amended it to include additional logic required to conditionally run the functionality based on whether or not the customizer is loaded with a customizer_blank_slate=on query parameter, for example:

http://example.com/wp-admin/customize.php?customizer_blank_slate=on

For more, see the Customizer Blank Slate plugin on GitHub.

7 thoughts on “Resetting the Customizer to a Blank Slate”

  1. Very nice. I was just thinking that Tailor (http://gettailor.com) could be a good use-case for something like this. It’s a Customizer-esque page builder that uses an`?tailor=1` query arg to load its own UI, which I assume is to avoid collisions with plugins and themes, and to future-proof itself from core a bit.

  2. Building something cool upon Customizer I had several discussions with you on how to pull this off. While my side project isn’t ready yet, I am going to use this snippet. It’s def better than what I had been building myself. Props for that.

  3. I think i will be going with your approach going forward.

    I have previously been hooking to the “customize_section_active” and “customize_panel_active” actions and returning the sections and panel i want displayed which in my opinion is quite elegant and not hacky.

    Customizer is great. Can’t wait to share what am building on top it to the world.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.