I'm trying to hide or show customizer settings based on what page I am viewing similar to active_callback' => 'is_front_page', however, I haven't found a way how to access the current page ID from my customizer.php file. I want to be able to use active_callback' => 'is_specific_page' through a custom callback based on current page ID like so:


    function is_specific_page() {

        // LOGIC RETURNS TRUE OR FALSE DEPENDING ON CURRENT PAGE

    }    

I've tried using all of the following to no avail:

global $post; $post->ID();

global $wp_query; $post_id = $wp_query->post->ID;

get_the_ID();

Thank you in advanced for your help!

Thanks for your question.

active_callback is exactly what you're looking for. You can use it with controls:

$wp_customizer->add_control(
    'setting_name',
    array(
        'type' => 'text',
        'section' => 'section_name',
        'label' => 'Option with context',
        'active_callback' => 'is_front_page'
    )
);

and with sections:

$wp_customize->add_section(
    'section-name',
    array(                                             
        'title' => 'Section with context',
        'active_callback' => 'is_front_page'
     )
);

In examples above, this new setting/section will be visible only for front page, thanks to native is_front_page function. You can also use other Conditional Tags.

But of course you can make your own contexts:

function mytheme_is_contact_page() {
    return is_page_template( 'template-contact.php' );
}

function mytheme_is_page_id_123() {
    return is_page( 123 );
}
  • is_page() seems to always return false in every case. Not sure what I’m doing wrong. – Alex E. Dec 10 at 15:06

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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