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 *

Your post must include a link to this post.

How does this work?
Write a response on your own website and include a link to this post. Then submit your response’s URL here. Your response may appear here after moderation. To update or remove it, edit or delete your original post and submit the same URL again. Learn more about Webmentions.