Adding More Sidebars to WordPress Themes

WordPress keeps being updated but your themes rarely change unless you’re a theme developer. I wanted to add a leaderboard but I was delaying because I am too lazy to add any custom code to my theme. Silly me I forgot that I have already added an extra “sidebar” when I developed the theme for this website. So all I had to do was paste my code to a text widget and save.

I know we are actually misusing the sidebar via the “dynamic_sidebar” and “register_sidebar” API. It’s fairly straightforward but you do need to have a sense of position in your layout (you need to know where the leaderboard needs to appear).

First we add the sidebar in the /wp-content/THEME/functions.php:

1
2
3
4
5
6
7
register_sidebar(array(
	'name' => 'Leaderboard',
	'before_widget' => '<div align="center">',
	'before_title' => '',
	'after_title' => '',
	'after_widget' => '</div>',
));

I think the options are self-explanatory but if you’re confused do ask. One important thing for me is the name – it will be displayed in the widget control panel so it would help to use a descriptive name.

The next file(s) to be edited depends on where you want the new “sidebar” to be located and how the theme are made. A simple standard is that if you want it to be included in all page you just edit /wp-content/THEME/header.php

dynamic_sidebar('Leaderboard');

Normally these are the file name convention used by theme developers:

  1. page.php – pages
  2. single.php – single posts
  3. search.php – search results
  4. index.php – the main page

Enjoy!

0 Shares