I’ve been getting really annoyed every time I want to add options to my theme and I have to copy a huge mess of code that just isn’t fun to use. You know what I mean. Most theme developers now include configurable options in their themes, which if you ask me, isn’t super easy.

So the other day I set out to write a class that theme developers could package with their file and have easy access to theme options. It’s as easy as adding my theme_options.php file to an include folder in your theme directory, and then adding this code to your functions.php (Using your option values obviously).

Isn’t that easier that what you’ve been doing? I’ve encapsulated my code into a nice class to make everything super simple. The results should look similar to this (Located in Appearance > Theme Options):

I released my my walker class as a super easy to use plugin (This doesn’t even require you to change your wp_nav_menu commands as it uses a filter to add it). I’ve submitted it to WordPress.org so I’ve linked to it below.

Just install and activate it, and then you can go to a page and mark it as Members Only.

Now featuring full support for wp_page_menu AND wp_nav_menu. Tested on WordPress 2.9+. Also fully compatible with PHP 4+!

Walkers are used in WordPress to recursively handle data such as menus. As defined in the WordPress codex

The walker class encapsulates the basic functionality necessary to output HTML representing WordPress objects with a tree structure. For instance pages and categories are the two types of objects that the WordPress 2.2 code uses the walker class to enumerate. While one could always display categories as you wished by using right func and looping through them using it takes a great deal of work to arrange subcategories below their parents with proper formatting. The walker class takes care of most of this work for you.

Now, unfortunately there is very little good documentation about Walker classes, even though they give you more customization and flexibility then the default code, all without modifying core WordPress files which is perhaps the most important part. I set out this last week to allow my client to create a members only page which he could add to the site navigation, and have it only show up if the user was logged in. This turned out to be more difficult than I initially expected as private pages cannot be added to the menu.

After searching the Internet and finding nothing, I did the next best thing: Searched through the WordPress code. I eventually came up with this, which adds a checkbox saying “Is Members Only?” to the page editor. Then I added some code to my theme’s functions.php, which could easily be put in a plugin instead which checks to make sure the user is logged in. Now to make it selectively appear in the menus, I wrote my only walker class, extending the default one.

This code is also fairly straight forward. We add a meta box to the page editor (Look at the add_meta_box, I specify page). We also add some code to save our post meta, prefixing it with an underscore so it is hidden from the meta box interview.

This is almost exactly the same as the default nav function, except I added the line “if ( !get_post_meta( $item->object_id, ‘_members_only’ , true ) || is_user_logged_in() ) {“. Now, in this case I wanted to modify only the Nav Walker, without editing core files. I didn’t need to change much, so all I did was extend the default Nav Walker, instead of the base Walker class.

If you look through the code, it’s not really hard to understand. The walker class has several functions, start_el, end_el, walk, start_lvl, and end_lvl. You can extend the base classes and implement your own versions of these functions if you want. I’ll explain what these do.

start_lvl – Creates the <ul>
end_lvl – Creates the </ul>
start_el – Creates the <li right to the </li> but excluding the </li>
end_el – Creates the </li>
walk – This is a fairly complex function that calls the above functions depending on if a submenu needs to be created, or closed, etc.

I would recommend looking at the Walker class in wp-includes/classes.php and the other classes that extend the Walker class from wp-includes/nav-menu-templates.php.

Now the most important part is getting your code to use the new walker class. This is super simple:

I’ve been using WordPress 3.0 at work for a month now, and what do I think of it? It’s fantastic! WordPress 3.0 added many new features, including custom menus which are extremely helpful for using WordPress as a CMS. WordPress 3 implemented over 200 fixes and features, so I recommend checking out the official site to see a list of all of them. Here’s the ones I like the most

WordPress Menus – Add menus across your site that are manageable via the admin and are as simple as calling wp_nav_menu in your theme.

Custom Headers – You can set the header in your blog via a nice admin panel

Custom Post Types – These are the most important feature for sure. It allows you to easily (Like 30 lines of code easy) add your own post types. Like videos, services, products, etc. Basically, you pass a few arguments to register_post_type and you’re done. WordPress auto generates an interface similar to the posts/pages interface. You can extend it with your own version of categories and tags as well (For example, you could make a gallery tag/category taxonomy for grouping your video post types).

With the amount of development I’ve been doing, I’ve noticed a lot of really useful functions, actions, and filters have no documentation on the WordPress codex, and some have no documentation anywhere that I could find (Like the Walker Class and making custom Walkers). I’ve registered a codex account and am slowly adding documentation, and any of the really awesome stuff I’ll post here as well.