0

I would like to have something like:

if (is_post_type_archive('my_post_type')) {
    $wp_customize->add_section(...);
    $wp_customize->add_setting(...);
    $wp_customize->add_control(...);
}

I have already seen similar question (Adding Controls to Theme Customizer If Certain Page Template is Active), it refers to Wordpress 4.0, that there would be available this setting. It's Wordpress 4.9 now, and this didn't included yet.

Also, I have read, that I can not do this, because when customizer registers, there still no object $wp_query. So, how can I archieve such behavior?

  • 1
    this code from customizer API defines the control in the administration page. it's when you use it in template that you test if you are on the specific page. – mmm Apr 15 '18 at 9:43
  • As far as I can tell active_callback is available, Twenty Seventeen uses it. Have you tried it? – Jacob Peattie Apr 15 '18 at 10:49
  • @mmm, of course, I know it :) And I would like to show one controls, when theme customizer opened on main page and the others - when customizer opened on this is_post_type_archive('my_post_type') page – Александр Чи Apr 15 '18 at 12:26
  • @JacobPeattie, what is active callback? I will google it now and try to look to Twenty Seventeen functions.php – Александр Чи Apr 15 '18 at 12:27
  • @JacobPeattie, I found out! The active_callback is what I need! Thank you very much. You should write this as an awnser so I can accept it :) – Александр Чи Apr 15 '18 at 13:06
1

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',
) );
  • By the way, I often see such function names as this wpse_300815_is_my_post_page. It has some abbreviation in the beginning and some number in title. Is there any generator of functions, or why people call it like that? – Александр Чи Apr 16 '18 at 15:08
  • It's a good practice in WordPress to prefix functions with something specific to your project to avoid conflicts with other code. On this site it's common to add a unique prefix to avoid giving people code that could cause conflicts if copied and pasted directly. This is usually wpse_ (WordPress Stack Exchange), and then the ID of the question, 300815_ (you can see the ID in the URL). The addition of the ID helps avoid conflicts if a user is trying multiple bits of code from this site at once, and if pasted unmodified it gives the user a reference to where they got it from. – Jacob Peattie Apr 16 '18 at 15:19
  • wow, didn't noticed that before! Thanks again – Александр Чи Apr 17 '18 at 15:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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