0

In my customizer panel I have a radio button for hiding or showing a feature on my WordPress site. However I need to pass multiple values when a single option is selected.

e.g.

enter image description here

the related sections in my functions.php looks like this:

$wp_customize-> add_control('blog_setting', array(
    'label' => __("Turn on / off the blog link", 'portfolio'),
    'section' => 'Layout_options',
    'settings' => 'blog_setting',
    'type' => 'radio',
    'choices' => array(
            'block' =>   'Blog link visible',
            'none' =>   'Blog link hidden'
        ) 
   ) );

and I change the css with this value like this:

function css_customizer(){
?>

<style type="text/css">
    .blog__Link{display: <?php echo get_theme_mod('blog_setting'); ?>}
</style>
<?php
}

add_action('wp_head', 'css_customizer');




However I need to implement something like this (note **)

 $wp_customize-> add_control('blog_setting', array(
        'label' => __("Turn on / off the blog link", 'portfolio'),
        'section' => 'Layout_options',
        'settings' => 'blog_setting',
        'type' => 'radio',
        'choices' => array(
                'block', '1'** =>   'Blog link visible',
                'none', '0'**  =>   'Blog link hidden'
            ) 
       ) );

and access the second value like this:

function css_customizer(){
?>

<style type="text/css">
    .blog__Link{display: <?php echo get_theme_mod('blog_setting[0]**'); ?>}
    .second_css_class{opacity: <?php echo get_theme_mod('blog_setting[1]**'); ?>}
</style>
<?php
}

add_action('wp_head', 'css_customizer');

Is this possible? I understand that it is possible to use javascript-theme-customizer, but this seems to be over complicated, I simply need to pass multiple values for each option, and be able to access them. thanks.

1 Answer 1

1

I understand what you are trying to do, but you are going in the wrong direction here, you can just check if one was selected in your logic and assign the value there like this:

function css_customizer(){
  $blog_setting_val = get_theme_mod('blog_setting');
  $opacity_val = ($blog_setting_val == 'block') ? '1' : '0';
?>

<style type="text/css">
    .blog__Link{display: <?php echo $blog_setting_val; ?>}
    .second_css_class{opacity: <?php echo $opacity_val; ?>}
</style>
<?php
}

add_action('wp_head', 'css_customizer');

that should do it for your particular case.

2
  • Considerations: If you only have 2 options, do you need a radio button? what about just a checkbox. Instead of using CSS, wouldnt be better to not output the HTML with a PHP IF?
    – David Lee
    Commented Mar 22, 2017 at 21:48
  • thank you for your solution and your comment, I haven't tested this yet but it looks like a clear solution. +1
    – esd
    Commented Mar 22, 2017 at 22:54

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.