0

I have a function on my functions.php that adds a bootstrap col class to the footer sidebars depending on the number of active sidebars.

What I intend to achieve with this is having footer widgets expand to the maximum space available. So, for example if I have only one footer widget area active a class of col-md-12 with be applied. If I have two a class of col-md-6 would be applied and so on. This is my code:

// Count the number of footer sidebars to enable dynamic classes for the footer top row.
if( ! function_exists( 'mytheme_footer_sidebars_top' ) ) {
    function mytheme_footer_sidebars_top() {

        $active_sidebars = 0;

        if ( is_active_sidebar( 'footer-sidebar-1' ) ) {
            $active_sidebars++;
        }

        if ( is_active_sidebar( 'footer-sidebar-2' ) ) {
            $active_sidebars++;
        }

        if ( is_active_sidebar( 'footer-sidebar-3' ) ) {
            $active_sidebars++;
        }

        if ( is_active_sidebar( 'footer-sidebar-4' ) ) {
            $active_sidebars++;
        }

        $class = '';

        switch ( $active_sidebars ) {
            case '1':
                $class = ' col-sm-12';
                break;
            case '2':
                $class = ' col-sm-6';
                break;
            case '3':
                $class = ' col-sm-4';
                break;
            case '4':
                $class = ' col-sm-3';
                break;
        }

        echo $class;
    }
}

// Count the number of footer sidebars to enable dynamic classes for the footer bottom row.
if( ! function_exists( 'karnivorz_footer_sidebars_bottom' ) ) {
    function karnivorz_footer_sidebars_bottom() {

        $active_sidebars = 0;

        if ( is_active_sidebar( 'footer-sidebar-5' ) ) {
            $active_sidebars++;
        }

        if ( is_active_sidebar( 'footer-sidebar-6' ) ) {
            $active_sidebars++;
        }

        if ( is_active_sidebar( 'footer-sidebar-7' ) ) {
            $active_sidebars++;
        }

        if ( is_active_sidebar( 'footer-sidebar-8' ) ) {
            $active_sidebars++;
        }

        $class = '';

        switch ( $active_sidebars ) {
            case '1':
                $class = ' col-sm-12';
                break;
            case '2':
                $class = ' col-sm-6';
                break;
            case '3':
                $class = ' col-sm-4';
                break;
             case '4':
                 $class = ' col-sm-3';
                 break;
        }

        echo $class;
    }
}

then my sidebar-footer.php looks like this:

<?php
    // Check if the any of the footer widget areas has widgets.
    if ( ! is_active_sidebar( 'footer-sidebar-1' )
            && ! is_active_sidebar( 'footer-sidebar-2' )
            && ! is_active_sidebar( 'footer-sidebar-3' )
            && ! is_active_sidebar( 'footer-sidebar-4' )
            && ! is_active_sidebar( 'footer-sidebar-5' )
            && ! is_active_sidebar( 'footer-sidebar-6' )
            && ! is_active_sidebar( 'footer-sidebar-7' )
            && ! is_active_sidebar( 'footer-sidebar-8' )
    )
    return;

    // Check which footer widget areas are active and apply formatting classes to them according to the number of active areas.
    ?>
    <div class="footer-sidebar" role="complementary">
        <div id="footer-top-sidebar-container" class="container">
            <div class="row">
                <?php if ( is_active_sidebar( 'footer-sidebar-1' ) ) : ?>
                    <div class="footer-widget-area col-xs-12 <?php mytheme_footer_sidebars_top(); ?>">
                        <?php dynamic_sidebar( 'footer-sidebar-1' ); ?>
                    </div><!-- end footer-widget-area -->
                <?php endif; ?>

               <?php if ( is_active_sidebar( 'footer-sidebar-2' ) ) : ?>
                   <div class="footer-widget-area col-xs-12 <?php mytheme_footer_sidebars_top(); ?>">
                      <?php dynamic_sidebar( 'footer-sidebar-2' ); ?>
                   </div><!-- end footer-widget-area -->
               <?php endif; ?>

               <?php if ( is_active_sidebar( 'footer-sidebar-3' ) ) : ?>
                   <div class="footer-widget-area col-xs-12 <?php mytheme_footer_sidebars_top(); ?>">
                       <?php dynamic_sidebar( 'footer-sidebar-3' ); ?>
                   </div><!-- end footer-widget-area -->
               <?php endif; ?>

               <?php if ( is_active_sidebar( 'footer-sidebar-4' ) ) : ?>
                   <div class="footer-widget-area col-xs-12 <?php mytheme_footer_sidebars_top(); ?>">
                       <?php dynamic_sidebar( 'footer-sidebar-4' ); ?>
                   </div><!-- end footer-widget-area -->
              <?php endif; ?>
          </div><!-- end row -->
     </div><!-- end container -->

<div id="footer-bottom-sidebar-container" class="container">
    <div class="row">
        <?php if ( is_active_sidebar( 'footer-sidebar-5' ) ) : ?>
        <div class="footer-widget-area col-xs-12 <?php mytheme_footer_sidebars_bottom(); ?>">
            <?php dynamic_sidebar( 'footer-sidebar-5' ); ?>
        </div><!-- end footer-widget-area -->
        <?php endif; ?>

        <?php if ( is_active_sidebar( 'footer-sidebar-6' ) ) : ?>
        <div class="footer-widget-area col-xs-12 <?php mytheme_footer_sidebars_bottom(); ?>">
            <?php dynamic_sidebar( 'footer-sidebar-6' ); ?>
        </div><!-- end footer-widget-area -->
        <?php endif; ?>

        <?php if ( is_active_sidebar( 'footer-sidebar-7' ) ) : ?>
        <div class="footer-widget-area col-xs-12 <?php mytheme_footer_sidebars_bottom(); ?>">
            <?php dynamic_sidebar( 'footer-sidebar-7' ); ?>
        </div><!-- end footer-widget-area -->
        <?php endif; ?>

        <?php if ( is_active_sidebar( 'footer-sidebar-8' ) ) : ?>
        <div class="footer-widget-area col-xs-12 <?php mytheme_footer_sidebars_bottom(); ?>">
            <?php dynamic_sidebar( 'footer-sidebar-8' ); ?>
        </div><!-- end footer-widget-area -->
        <?php endif; ?>
    </div><!-- end row -->
</div><!-- end container -->

This works fine for a single row of widget areas. But, what I want to achieve now is to be able to have another row exactly with the same functionality so the users could also add multiple rows of widgets in the footer. If I repeat exactly the same code and have another function called mytheme_footer_sidebars_bottom, for example, I will be able to achieve this but I am sure its not the best way as I will be repeating and renaming code. So I am wondering whats the best way to achieve this functionality without repeating all the code I have above.

2
  • Why do you need a second row specifically? If a user adds a second widget to any column in the existing would it not be stacked vertically anyway?
    – majick
    Feb 8, 2016 at 2:18
  • Yes, you're right. But, the reason for the second row is that any second widget added to any column on the first row will be the width of that column and I would like to be able to have different width widget areas on a second row.
    – Madeirense
    Feb 8, 2016 at 11:27

1 Answer 1

1

Just add a variable to the function so you can call it differently from the template...

EDITED: pass the footer row to function and return class...

function mytheme_footer_sidebar_class($row) {

    if ($row == 1) {$i = 1; $j = 5;} 
    elseif ($row == 2) {$i = 5; $j = 9;}

    $active_sidebars = 0;

    for ($i; $i < $j; $i++) {
        if ( is_active_sidebar( 'footer-sidebar-'.$i ) ) {
            $active_sidebars++;
        }
    }

    switch ( $active_sidebars ) {
        case '1':
            $class = ' col-sm-12';
            break;
        case '2':
            $class = ' col-sm-6';
            break;
        case '3':
            $class = ' col-sm-4';
            break;
         case '4':
             $class = ' col-sm-3';
             break;
    }

    return $class;
}

And at the top of footer-sidebar.php you can just do this once instead of repeating the checks all over again:

$rowclass1 = mytheme_footer_sidebar_class(1);
$rowclass2 = mytheme_footer_sidebar_class(2);

if ( ($rowclass1 == '') && ($rowclass2 == '') ) {return;}

Then in place of <?php mytheme_footer_sidebars_top(); ?> and <?php mytheme_footer_sidebars_bottom(); ?> use <?php echo $rowclass1; ?> and ` respectively.

You can also then use the same variables to wrap and display each row...

 <?php if ($rowclass1 != '') : ?>
 <div id="footer-top-sidebar-container" class="container">
     .......................
 </div>
 <?php endif; ?>

...of course you would need to register 4 more widget areas... this could get confusing for the end user too on the widget page also, so you may want to add a theme option to turn the second footer widget area on/off.

4
  • Thanks for your feedback! I have edited my question and included all the code I have at the moment. So, at the moment it is working exactly as I want but I can't stop thinking I am repeating a lot of code. Even though it works fine I am wondering how can I reformat this code in order to achieve the same functionality but in a cleaner more concise way.
    – Madeirense
    Feb 8, 2016 at 11:58
  • see edits, a lot more concise now! :-)
    – majick
    Feb 8, 2016 at 12:19
  • Thanks man. Nothing seems to show and I am getting the following error: Fatal error: Maximum execution time of 60 seconds exceeded in C:\xampp\htdocs\mytheme\wp-includes\formatting.php on line 1090..
    – Madeirense
    Feb 8, 2016 at 13:10
  • my bad, endless for loop, should be ok now...
    – majick
    Feb 8, 2016 at 13:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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