How to add theme customizer control to specific page?

It’s possible to define a callback function that will be used to determine if the control is visible. It’s used the same way functions are used in action and filter hooks but you use it as the value for the active_callback of the control.

For example, to only show the control if is_front_page() is true, use it like this:

$wp_customize->add_control( 'front_page_greeting', array(
    'label'           => __( 'Greeting' ),
    'section'         => 'title_tagline',
    'active_callback' => 'is_front_page',
) );

If you need to pass an argument to the function, use the function in an anonymous function used as the callback:

$wp_customize->add_control( 'front_page_greeting', array(
    'label'           => __( 'Greeting' ),
    'section'         => 'title_tagline',
    'active_callback' => function() {
        return is_post_type_archive( 'my_post_page' );
    },
) );

Or define it separately:

function wpse_300815_is_my_post_page() {
    return is_post_type_archive( 'my_post_page' );
}

Then use it by name:

$wp_customize->add_control( 'front_page_greeting', array(
    'label'           => __( 'Greeting' ),
    'section'         => 'title_tagline',
    'active_callback' => 'wpse_300815_is_my_post_page',
) );

Leave a Comment