• I’m working on my theme customizer and am trying to find a more efficient way of showing a div

    Currently, what works for me is:

    (this is the section)

    $wp_customize->add_section('social-networks', array(

    (and this is what im using to display a div if any options are selected)

    <?php 
    
      if (    
    
        ( !empty(get_theme_mod('facebook-link')) ) 
    
        || 
    
        ( !empty(get_theme_mod('instagram-link')) ) 
    
        ||
    
        ( !empty(get_theme_mod('linkedin-link')) ) 
    
    ) : 
    
    ?>
    
    <div class="display-social-div"></div>
    
    <?php endif; ?>

    This works, but is there a simpler way to show the div if any of the setting/controls are checked inside the social-networks section, instead of having to list out each setting/control to see if they are checked? I have about 20 setting/controls and only showed 3 as an example here….

    • This topic was modified 6 years, 8 months ago by Shane Taylor.
Viewing 8 replies - 1 through 8 (of 8 total)
  • A better method than having an individual theme_mod for each link is for the theme to register a social menu, and then style it with the icons.
    That way
    1. the user doesn’t lose all their links when changing themes (they only have to enter them once),
    2. they get to put them in the order they prefer,
    3. they can add any links they want there,
    4. the links are not stored in theme options but in menu items (different database table) and so they are available for plugins to manipulate also.

    Other than that, you can use the active callback for code that decides whether to show the control or not. It splits the decision apart from the render.
    https://developer.wordpress.org/reference/classes/wp_customize_control/

    Thread Starter Shane Taylor

    (@propertunity)

    thanks for the reply, and I understand what you are saying, I’m just using social links as an example…

    im doing this for multiple things, besides theme links…. possibly colors, img bgs, etc….

    how would I go about doing this without listing each setting/control ?

    I’m looking for a better way to display a div if any theme_mods theme_options are selected in a specific panel section…

    • This reply was modified 6 years, 8 months ago by Shane Taylor.

    In both PHP and JS, you can access the Customizer manager and loop through all the controls or settings in a section or panel. Decide whether there is something you need from PHP or not (like content), but the easier way is to loop in JS, especially if it is showing or hiding something in the Customizer pane (which is only loaded once).
    But the active callback is the established method of using information from PHP (preview pane) to hide or show a control (Customizer pane). Also, the active callback does not have to be just hide/show. I have used it to choose ‘refresh’ or ‘postMessage’, but it will only return a boolean value unfortunately.

    Thread Starter Shane Taylor

    (@propertunity)

    ohhh… maybe I’m not saying this correctly… I’m not trying to conditionally/dynamically display anything in the backend customizer.php….

    I’m trying to display/hide a div on the frontend if theme_mods / theme_options are selected in a specific panel > section of the customizer…

    something like: (this is super hacky and I’m making this up)

    if !empty( panel > section > social_mods ) {

    then display div;

    endif;

    know what I mean?

    If it’s not in the Customizer, you just have to organize your options in a way that makes it easy to check, or code them all in a row or if statements.
    The Customizer section information is only available when in the Customizer. That code isn’t loaded otherwise.

    Moderator bcworkz

    (@bcworkz)

    You could grab all the theme mods at once with something similar to get_option('theme_mods_my-theme'). Then loop through all the elements with foreach ( $mods as $key => $val ). Then for each element check if $key is in an array of keys you want to check. If so, and $val is true or non-zero, then output the div and break out of the foreach loop. You would need to evaluate the array structure of the theme_mods option to decide which level of elements you need to loop through. It may not be the main elements you want but some inner array of elements.

    The advantage here is you are not repeatedly hitting the DB for individual values. Once one true value is found, there is no need to continue evaluating other array values.

    There is get_theme_mods function instead of using get_option('theme_mods_my-theme'). And since options are autoloaded and cached, you only ever hit the DB once anyway, even if you call get_theme_mod a bunch of times.

    Pay attention to this book about developing wordpress. There are many useful things in it, maybe you will find answers to questions, examples and codes there.

    • This reply was modified 6 years, 8 months ago by nadusha12.
Viewing 8 replies - 1 through 8 (of 8 total)

The topic ‘WP Customizer show div’ is closed to new replies.