Modifying an existing customizer control

This post shows you how to modify an existing customizer control — helpful for the solution was a Gist by Marcio Duarte.

In my case, I wanted to add an active_callback to the header image control to only show it if the user is on a static front page. To get this done, we only need one line of code:

/**
 * Set active callback for the header image control.
 */
$wp_customize->get_control( 'header_image' )->active_callback = 'slug_is_static_front_page';Code language: PHP (php)

You can modify the other control properties accordingly — a few examples can be found in the above-linked Gist. For the sake of completeness here is the callback function:

/**
 * Check if we are on a static front page.
 *
 * @param WP_Customize_Control $control Control object.
 *
 * @return bool
 */
function slug_is_static_front_page( $control ) {
	/**
	 * Return true if is static front page.
	 */
	if ( is_front_page() && is_page() ) {
		return true;
	} else {
		return false;
	}
}Code language: PHP (php)

Leave a Reply

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

To respond on your own website, enter the URL of your response which should contain a link to this post's permalink URL. Your response will then appear (possibly after moderation) on this page. Want to update or remove your response? Update or delete your post and re-enter your post's URL again. (Find out more about Webmentions.)