Advertisement
  1. Web Design
  2. HTML/CSS
  3. HTML

Dynamically Adding Four Footer Widget Areas

Scroll to top
Read Time: 11 min

These days, many WordPress themes have a number of widget areas in the footer, meaning you can create a "fat footer" with multiple widget areas side by side. It's something I use on all of my themes.

But what if your theme has four widget areas but you only want three widgets? If you're not careful, you'll end up with an ugly gap on the right-hand side where the empty fourth widget area is.

In this tutorial I'll show you how to avoid this using a combination of conditional tags in PHP and some Object-Oriented CSS (OOCSS), which will check how many widget areas you've populated and automatically resize them so they each take up the right proportion of the page's width.

What You'll Need

To follow this tutorial, you'll need:

  • a development installation of WordPress
  • your own theme to which you'll add the code
  • a code editor

Registering the Widget Areas

The first step is to register the four widget areas for your footer. If you don't already have any widget areas registered, you'll need to add this code to your functions.php file:

1
function tutsplus_widgets_init() {
2
3
    // First footer widget area, located in the footer. Empty by default.

4
	register_sidebar( array(
5
		'name' => __( 'First Footer Widget Area', 'tutsplus' ),
6
		'id' => 'first-footer-widget-area',
7
		'description' => __( 'The first footer widget area', 'tutsplus' ),
8
		'before_widget' => '<div id="%1$s" class="widget-container %2$s">',
9
		'after_widget' => '</div>',
10
		'before_title' => '<h3 class="widget-title">',
11
		'after_title' => '</h3>',
12
	) );
13
14
	// Second Footer Widget Area, located in the footer. Empty by default.

15
	register_sidebar( array(
16
		'name' => __( 'Second Footer Widget Area', 'tutsplus' ),
17
		'id' => 'second-footer-widget-area',
18
		'description' => __( 'The second footer widget area', 'tutsplus' ),
19
		'before_widget' => '<div id="%1$s" class="widget-container %2$s">',
20
		'after_widget' => '</div>',
21
		'before_title' => '<h3 class="widget-title">',
22
		'after_title' => '</h3>',
23
	) );
24
25
	// Third Footer Widget Area, located in the footer. Empty by default.

26
	register_sidebar( array(
27
		'name' => __( 'Third Footer Widget Area', 'tutsplus' ),
28
		'id' => 'third-footer-widget-area',
29
		'description' => __( 'The third footer widget area', 'tutsplus' ),
30
		'before_widget' => '<div id="%1$s" class="widget-container %2$s">',
31
		'after_widget' => '</div>',
32
		'before_title' => '<h3 class="widget-title">',
33
		'after_title' => '</h3>',
34
	) );
35
36
	// Fourth Footer Widget Area, located in the footer. Empty by default.

37
	register_sidebar( array(
38
		'name' => __( 'Fourth Footer Widget Area', 'tutsplus' ),
39
		'id' => 'fourth-footer-widget-area',
40
		'description' => __( 'The fourth footer widget area', 'tutsplus' ),
41
		'before_widget' => '<div id="%1$s" class="widget-container %2$s">',
42
		'after_widget' => '</div>',
43
		'before_title' => '<h3 class="widget-title">',
44
		'after_title' => '</h3>',
45
	) );
46
		
47
}
48
49
// Register sidebars by running tutsplus_widgets_init() on the widgets_init hook.

50
add_action( 'widgets_init', 'tutsplus_widgets_init' );

This will add four footer widget areas to your theme. If you view the Widgets screen you'll see them all there, along with any others you've already registered:

Adding the Widget Areas to Your Theme

If you add widgets to those widget areas now, nothing will happen. You need to add them to your theme's footer. I'll work through the code you need to add to your footer file in stages, adding more checks for different numbers of populated widget areas as we go along.

Note that you may need to alter some of the classes and elements containing the widget area code in line with your theme's structure and layout.

1. Checking if No Widget Areas Are Populated

The first step is to check if no widget areas are populated, in which case nothing should be output. Open your theme's footer.php file and add the code below.

1
<footer>
2
3
    <?php
4
	/* The footer widget area is triggered if any of the areas

5
	 * have widgets. So let's check that first.

6
	 *

7
	 * If none of the sidebars have widgets, then let's bail early.

8
	 */
9
	if (   ! is_active_sidebar( 'first-footer-widget-area'  )
10
		&& ! is_active_sidebar( 'second-footer-widget-area' )
11
		&& ! is_active_sidebar( 'third-footer-widget-area'  )
12
		&& ! is_active_sidebar( 'fourth-footer-widget-area' )
13
	)
14
		return;
15
16
	//end of all sidebar checks.

17
	endif;?>
18
		
19
</footer>

2. Checking if All Widget Areas Are Populated

Next, I'll check if all of the widget areas are populated. If this is the case, the theme will output the contents of all four, with each of them floated next to each other and taking up a quarter of the width of the screen. I'll add the classes for this here, but add the CSS to my stylesheet later.

After the line reading return; but before the endif; statement in the code you just added, add this:

1
if (   is_active_sidebar( 'first-footer-widget-area'  )
2
    && is_active_sidebar( 'second-footer-widget-area' )
3
	&& is_active_sidebar( 'third-footer-widget-area'  )
4
	&& is_active_sidebar( 'fourth-footer-widget-area' )
5
) :	?>
6
7
<aside class="fatfooter" role="complementary">
8
	<div class="first quarter left widget-area">
9
		<?php dynamic_sidebar( 'first-footer-widget-area' ); ?>
10
	</div><!-- .first .widget-area -->
11
12
	<div class="second quarter widget-area">
13
		<?php dynamic_sidebar( 'second-footer-widget-area' ); ?>
14
	</div><!-- .second .widget-area -->
15
16
	<div class="third quarter widget-area">
17
		<?php dynamic_sidebar( 'third-footer-widget-area' ); ?>
18
	</div><!-- .third .widget-area -->
19
20
	<div class="fourth quarter right widget-area">
21
		<?php dynamic_sidebar( 'fourth-footer-widget-area' ); ?>
22
	</div><!-- .fourth .widget-area -->
23
</aside><!-- #fatfooter -->

This does the following:

  • Checks that all footer widget areas are populated using if tags.
  • If so, opens a new element called <aside class = "fatfooter">. This extra element containing the widget areas allows the whole footer to be centred on the page even if the footer itself isn't (for example if the whole footer has a wide background).
  • Outputs all four widget areas using the dynamic_sidebar() function, giving each of these classes for layout.

The classes are .left.quarter for most of the widget areas, except for the last one which has .right.quarter.

3. Checking if Just Three Widget Areas Are Populated

The next step is to add the code to output the widget areas if just three of them are populated. Below the code you just added, insert the following:

1
<?php 
2
elseif ( is_active_sidebar( 'first-footer-widget-area'  )
3
    && is_active_sidebar( 'second-footer-widget-area' )
4
	&& is_active_sidebar( 'third-footer-widget-area'  )
5
	&& ! is_active_sidebar( 'fourth-footer-widget-area' )
6
) : ?>
7
<aside class="fatfooter" role="complementary">
8
	<div class="first one-third left widget-area">
9
		<?php dynamic_sidebar( 'first-footer-widget-area' ); ?>
10
	</div><!-- .first .widget-area -->
11
12
	<div class="second one-third widget-area">
13
		<?php dynamic_sidebar( 'second-footer-widget-area' ); ?>
14
	</div><!-- .second .widget-area -->
15
16
	<div class="third one-third right widget-area">
17
		<?php dynamic_sidebar( 'third-footer-widget-area' ); ?>
18
	</div><!-- .third .widget-area -->
19
20
</aside><!-- #fatfooter -->

This checks if the first three widget areas are populated and the fourth isn't, and then outputs the first three using appropriate classes—replacing .quarter with .one-third.

Note that when you're populating your widget areas, you always need to miss out the last one(s) if you don't want to use them all, or this check won't work. Don't leave the first one empty and populate the later ones.

4. Checking if Just Two Widget Areas Are Populated

To output just two widget areas if they've been populated and the third and fourth ones haven't, add this code below the code you just added:

1
<?php
2
elseif ( is_active_sidebar( 'first-footer-widget-area'  )
3
    && is_active_sidebar( 'second-footer-widget-area' )
4
	&& ! is_active_sidebar( 'third-footer-widget-area'  )
5
	&& ! is_active_sidebar( 'fourth-footer-widget-area' )
6
) : ?>
7
<aside class="fatfooter" role="complementary">
8
	<div class="first half left widget-area">
9
		<?php dynamic_sidebar( 'first-footer-widget-area' ); ?>
10
	</div><!-- .first .widget-area -->
11
12
	<div class="second half right widget-area">
13
		<?php dynamic_sidebar( 'second-footer-widget-area' ); ?>
14
	</div><!-- .second .widget-area -->
15
16
</aside><!-- #fatfooter -->

This works in a similar way to the previous two code snippets, using the .half class for layout.

5. Checking if Just One Widget Area is Populated

The final check is for just the first widget area being populated. Add this below the code you just added:

1
<?php
2
elseif ( is_active_sidebar( 'first-footer-widget-area'  )
3
    && ! is_active_sidebar( 'second-footer-widget-area' )
4
	&& ! is_active_sidebar( 'third-footer-widget-area'  )
5
	&& ! is_active_sidebar( 'fourth-footer-widget-area' )
6
) :
7
?>
8
<aside class="fatfooter" role="complementary">
9
	<div class="first full-width widget-area">
10
		<?php dynamic_sidebar( 'first-footer-widget-area' ); ?>
11
	</div><!-- .first .widget-area -->
12
13
</aside><!-- #fatfooter -->

This will output the one widget area's contents in a full-width element.

Now save your footer.php file.

Adding the CSS

Right now your widget areas won't work correctly—in fact, if you populate them all they'll appear one below the other at full width. So you need to add some OOCSS to make those layout classes work. The OOCSS I'm adding is responsive, so I've included media queries to resize the footer widget areas on small screens.

1. Adding Floats

First let's make sure the widget areas float alongside each other.

Open your theme's style.css file and add this code:

1
/* floats */
2
.quarter,
3
.one-third,
4
.two-thirds,
5
.half {
6
    float: left;
7
}

2. Adding Widths

The widths will ensure that all of the widget areas take up the correct proportion of the screen. I'm using percentages as it makes the code more flexible and responsive. Add this to your stylesheet:

1
/* widths */
2
.one-third {
3
    width: 32%;
4
}
5
.two-thirds {
6
	width: 65.5%;
7
}
8
.quarter {
9
	width: 23.5%;
10
}
11
.three-quarters {
12
	width: 74.5%;
13
}
14
.half {
15
	width: 48%;
16
}

Note that the widths don't add up to 100% because space needs to be allowed for margins.

3. Adding Margins

Now add the styling for margins to your stylesheet:

1
/* margins  */
2
.one-third {
3
    margin: 0 0.5%;
4
}
5
.quarter,
6
.two-thirds {
7
	margin: 0 0.5%;
8
}
9
.left,
10
.quarter.left,
11
.one-third.left {
12
	margin: 0 1% 0 0;
13
	float: left;
14
}
15
.right,
16
.quarter.right,
17
.one-third.right {
18
	margin: 0 0 0 1%;
19
	float: right;
20
}
21
.half.left {
22
	width: 48%;
23
	margin: 0 2% 0 0;
24
}
25
.half.right {
26
	width: 48%;
27
	margin: 0 0 0 2%;
28
}
29
.two-thirds.left {
30
	margin: 0 1% 0 0;
31
}
32
.two-thirds.right {
33
	margin: 0 0 0 1%;
34
	float: right;
35
}

Note that the margins are different for elements which also have the .right or .left classes applied to them, so that they sit flush at the edge of their containing element.

4. Adding Media Queries

The media queries will do two things:

  • On small screens such as tablets, four elements will appear in a two by two grid, meaning that elements with the .quarter class will have half width and margins will be adjusted accordingly.
  • On very small screens such as smartphones, all elements will be full width.

Add the media queries at the bottom of your stylesheet along with any other media queries you may have:

1
/* media queries for larger screens such as small tablets in landscape or large tablets in portrait */
2
@media screen and ( max-width: 780px ) {
3
    
4
	/* only the .quarter layout class is relevant here - all other classes will have full width */
5
	.quarter {
6
		width: 48%;
7
	}
8
	.quarter.left {
9
		margin-right: 2%;
10
	}
11
	.quarter.right {
12
		margin-left: 2%;
13
	}
14
	footer .third.quarter.widget-area {
15
		clear: both;
16
	}
17
}
18
19
/* media queries for small screens in landscape mode (or similar) */
20
@media screen and ( max-width: 600px ) {
21
	
22
	/* width sizing all full width in small screens */
23
	.quarter,
24
	.one-third,
25
	.half,
26
	.two-thirds,
27
	.three-quarters,
28
	.full-width {
29
		width: 100%;
30
		margin: 0;
31
	}
32
	/* padding adjustments */
33
	.widget-area {
34
		padding: 0 0 10px 0;
35
	}
36
}

Now save your stylesheet.

Testing Your Code

Now it's time to see what happens when you populate your widget areas.

Below is a screenshot of a site I manage which makes use of this code. Normally the footer has four widget areas:

Footer with four widget areasFooter with four widget areasFooter with four widget areas

If I remove the widget from the fourth widget area, just three are displayed, with equal widths:

Footer with three widget areasFooter with three widget areasFooter with three widget areas

And if I remove widgets from the third widget area, visitors will see two widget areas each taking up half of the width:

Footer with two widget areasFooter with two widget areasFooter with two widget areas

Summary

This technique is useful if you want your theme to allow for the population of multiple footer widget areas but you don't know how many of those widget areas will actually be used.

If you're creating a framework, a starter theme, or a theme which other people will use for their sites, it can help make the footer layout tidier and save extra work down the line adding layout CSS.

Advertisement
Did you find this post useful?
Want a weekly email summary?
Subscribe below and we’ll send you a weekly email summary of all new Web Design tutorials. Never miss out on learning about the next big thing.
Advertisement
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.