1

I'm trying to show a text area in my customizer, only when a checkbox is selected. I followed instruction at this post, but my textarea stays hidden. Any help will be super appreciated! Thank you

function map_enabled(){
$tsum_options = get_theme_mod( 'tsum_options');
if( empty( $tsum_options['map_code'] ) ) {
    return false;
}
return true;
}


function tsum_customize_register( $wp_customize ) {

$wp_customize->add_setting( 'tsum_options[show_map]', array(
    'default'    => false,
    'capability' => 'edit_theme_options',
    ));

    // FRONT BOX CHECK - CONTROL
    $wp_customize->add_control( 'show_map', array(
    'label'      => 'Show Goole Map?',
    'section'    => 'tsum_contact_section',
    'settings'   => 'tsum_options[show_map]',
    'type'       => 'checkbox',
    //'priority'   => 20,
    ));  

    // FRONT BOX TEXT - SETTING
    $wp_customize->add_setting('tsum_options[map_code]', array(
    'default'        => '<iframe src=... ',
    'capability'           => 'edit_theme_options',
    ));

    // FRONT BOX TEXT - CONTROL
    $wp_customize->add_control('map_code', array(
    'label'   => 'lll',
    'description' => 'To embed a map, follow the steps below.<br>
                        1.) Open Google Maps. <a href="https://www.google.com/maps" target="_blank">Click here.</a><br>
                        2.) Search for your Location.<br>
                        3.) Zoom in - optional.<br>
                        4.) Change to Earth View - optional.<br>
                        5.) Click on Share<br>
                        6.) Click on Embed<br>
                        7.) Select Size => Custom Size.<br>
                        8.) Type in: 980  x  300<br>
                        9.) Copy the iframe code<br>
                        10.) Paste it below<br>
                        11.) Click on Save and Publish',  
    'section' => 'tsum_contact_section',
    'type'    => 'textarea',
    'setting' => 'tsum_options[map_code]',
    'active_callback' => 'map_enabled',
    )); 
}
add_action( 'customize_register', 'tsum_customize_register' );
0

2 Answers 2

0

In your second line you try to get the array of all mods with:

$tsum_options = get_theme_mod( 'tsum_options');

This will always return unset, because if you want all mods you need (note the plural):

$tsum_options = get_theme_mods( 'tsum_options');

Alternatively you could immediately check the relevant mod with

$tsum_option = get_theme_mod( 'tsum_options[show_map]');

2
  • Changing to plural didn't work for my. I am not 100% sure how to work with the 2nd suggestion. Thank you anyway, much appreciated! Jun 5, 2016 at 19:22
  • Well, you are storing your mods in an array called $tsum_options. In your function map_enabled you are trying to get the whole array from the database and then find an element in it. If the array returns empty, you won't get the element. My 2nd suggestion is not to get the array from the database, but only the element you need. So you would change the function like this: $tsum_map_option = get_theme_mod( 'tsum_options[show_map]'); if( empty( $tsum_map_options ) )
    – cjbj
    Jun 6, 2016 at 7:51
0

Maybe you could try something like this instead, ie. using the $control object to get the live value rather than the stored one:

function map_enabled($control) {
    $option = $control->manager->get_setting('tsum_options[show_map]');
    if (empty($option->value()) {return false;}
    return true;
}
2
  • I tried both your suggestions, but it didn't work. Most likely due to my limited understanding of what I'm busy with :) I'll play a bot more with your suggestions, just maybe... Jun 5, 2016 at 19:20
  • you probably need to refresh the control preview pane for active_callback to checked again, as it is PHP and already processed for the pageload, it will only work on refresh? if you want it to work live you will need to use transport => 'postMessage' with javascript similar to this: wordpress.stackexchange.com/a/211953/76440
    – majick
    Jun 6, 2016 at 14:02

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.