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)