php - How to disable controls in theme customizer?

admin2025-01-07  4

Edit: I have rewritten the question in hopes I will get a reply from someone who knows the theme customizer.

I am trying to figure out how to disable setting controls in the Wordpress theme customizer page. An example is:

  1. A section in the theme customizer contains a checkbox that is used to hide or show text in a hero container. When it is checked it will hide the text. The checkbox label says "Hide hero text".

  2. Within the same section there is a textbox for the user to enter the text they want for the hero container. The label says "hero text".

I am trying to figure out how to disable the textbox(2.) when the checkbox(1.) is checked. By disabling I am referring to making the textbox inactive so nothing can be input into it.

Can anyone explain how this can be done? Information on the theme customizer is very scarce at this time. I would greatly appreciate it.

Edit: I have rewritten the question in hopes I will get a reply from someone who knows the theme customizer.

I am trying to figure out how to disable setting controls in the Wordpress theme customizer page. An example is:

  1. A section in the theme customizer contains a checkbox that is used to hide or show text in a hero container. When it is checked it will hide the text. The checkbox label says "Hide hero text".

  2. Within the same section there is a textbox for the user to enter the text they want for the hero container. The label says "hero text".

I am trying to figure out how to disable the textbox(2.) when the checkbox(1.) is checked. By disabling I am referring to making the textbox inactive so nothing can be input into it.

Can anyone explain how this can be done? Information on the theme customizer is very scarce at this time. I would greatly appreciate it.

Share Improve this question edited May 15, 2013 at 2:25 user1632018 asked May 1, 2013 at 4:29 user1632018user1632018 4843 gold badges10 silver badges26 bronze badges 2
  • 2 Please provide more details. Maybe a more specific example of what you're trying to accomplish? – gdaniel Commented May 1, 2013 at 13:20
  • I am trying to figure out how to disable controls in the theme customizer. An example is if a checkbox in a section of the theme customizer is checked then it will automatically disable certain controls that I specified. Such as a button. – user1632018 Commented May 2, 2013 at 2:48
Add a comment  | 

2 Answers 2

Reset to default 0

I have not tested this yet but plan to do something similar soon..

If you want to completely remove the default control you can use this code. You can find the appropriate control id to use in place of 'display_header_text' by searching for the correct one that matches the control you wish to target in wp-includes/class-wp-customize-manager.php

$wp_customize->remove_control('display_header_text');

Display_header_text is the checkbox which toggles whether or not Site title and tagline are displayed.

If (for example) you want to actually hide the Site title textbox control in the theme customizer when display_header_text is checked you can do something like this..

if( get_theme_mod( 'display_header_text' ) !== '') {
    $wp_customize->remove_control('blogname');
} else {
    $wp_customize->add_setting( 'blogname', array(
        'default' => 'default_value',
    ) );
    $wp_customize->add_control( 'blogname', array(
        'label'   => 'Title',
        'section' => 'title_tagline',
        'type'    => 'text',
        'priority' => 1
    ) );
}

The theme customizer stores the value of a checkbox as the numeral 1 if it is checked and blank if it isn’t checked. So the code above says if the checkbox which controls whether or not site title and subtitle are displayed, is not empty then remove the site title textbox so the option to enter a site title won't even be available. But if the checkbox is empty, display the textbox.

If hiding the text box is acceptable, you can use the active_callback argument:

function should_display_hero_text() {
    return ! get_theme_mod( 'hide_hero_text', false );
}

// ...
$wp_customize->add_control(
    'hide_hero_text',
    array(
        'type' => 'checkbox',
        // ...
    )
);
$wp_customize->add_control(
    'hero_text',
    array(
        'type' => 'text',
        'active_callback' => 'should_display_hero_text',
        // ...
    )
);

This would make the hero_text field appear and disappear depending hide_hero_text's value.

Making an input disabled or readonly doesn't seem to be documented, but you may want to take a look at the input_attrs option from WP_Customize_Control::__construct.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736265314a1036.html

最新回复(0)