2

Heyo,

is it possible to show different Customizersettings in the Customizer API when you switch between two pages and each page has a different Template?

For example: Page-1 has the template Custom-Sidebar and Page-2 has the template Custom-Fullwidth. Now when I go into the Customizer and view Page-1 I want to show Settings for the Sidebar and when I change to Page-2 I want to show Settings for Images and other stuff ... but not for the Sidebar! (If that makes sense)

Thanks in advance!

1 Answer 1

3

The customizer API offers active_callback methods on both the control andd section classes. Basically you use either an conditional or a custom function to determine if a control or section is shown to the user.

If you want a section to appear only for pages, you would do somthing like this:

$wp_customize->add_section( 'wpse_283821_acme_pages', array(
    'title' => 'Acme Pages Section',
    'description' => 'Edit the ACME Pages Sections',
    'priority' => 20,
    'active_callback' => 'is_page',
) );

Or maybe a specific custom function:

$wp_customize->add_section( 'wpse_283821_acme_special_page', array(
    'title' => 'Acme Special Page Section',
    'description' => 'Edit the Specific ACME Page Section',
    'priority' => 20,
    'active_callback' => 'wpse_283821_acme_custom_callback',
) );

wpse_283821_acme_custom_callback() {
    // Do your logic here
    if ( true !== $condition ){
        return false;
    } else {
        return true;
    }
}

There are some questions on WPSE with good answers showing interesting uses of active_callback. Check this one and this one. And there is this post with a use case just like yours.

I could just copy them here, but i Think it's worth taking a look at those questions, answers, comments and code to get a deeper understanding.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.