Like the blog? Get the book »

You are reading an exclusive guest post by  »

Don’t fork your theme, flex it with “is_plugin_active” conditional

Don’t fork your theme, flex it with “is_plugin_active” conditional

Donkey work is really the last thing I want to be doing. Piddly tasks that could have been avoided with a little thought and perspective. In this DigWP tutorial, I explain how I worked my way away from becoming a donkey with a dozen child themes to manage and maintain, with just a little knowledge of a native WordPress function.

The details.

I am running a multisite that predominately uses a single child theme. Each site has it’s separate color scheme applied through a few lines of css. Where sites need unique functionality, functionality plugins are used. These contain custom post types, rss redirects, custom thumbnail sizes etc… This has worked really well for keeping our theme’s functions.php file slim while giving us all the functionality we need, where we need it.

Each site uses a single identical footer, which is built into the theme. At the time, we did this knowing that we would be creating department sites as subdirectories. Content between the utility navigation and the footer was to be their turf but the utility nab and footer content are locked down. We built our universal footer using switch_to_blog function and a custom menu.

<?php
	global $blog_id;
		global $current_blog;
		switch_to_blog(25);
        	$args = array(
			'container'    => 'div',
			'menu'         => 'Footer',
			'container_id' => 'footer-links',
			'menu_class'   => 'menu', 
			'echo'         => true,
			'fallback_cb'  => 'wp_page_menu',
			'depth'        => 0
		);
		wp_nav_menu($args);
		restore_current_blog(); ?>
		
<div class="fix"></div>
<script type="text/javascript">
jQuery(document).ready(function() {
	jQuery("ul#menu-footer.menu > li > a").contents().unwrap();
	jQuery('input[type=text]').focus(function() {
		jQuery(this).val('');
	});
});
</script>

The above code registers a new menu area called Footer. To take advantage of this I need to create a menu in the wordpress menu area with that exact name. The bit of JQuery turns off the link in the first level of menu items. This with a little css is used to create the menu headers. Great! One identical footer that is used on every site that uses this child theme.

The Dilemma

Recently we offered to host/manage websites for regional offices in our organization. They would benefit from our existing infrastructure and consistent branding but all of the content and navigation would be their own. They would not need our built in footer, at least not as it is on all the other sites.

I considered having a separate theme that was modified just for regional offices, I also considered building a plugin that disabled and replaced the footer on the existing theme. The first option would not be sustainable in the long run, the second very complicated at the outset. To be honest I very much doubt I could have pulled it off. My solution was to take advantage of the fact that I had already created a functionality plugin that would be used on every regional site. Using the is_plugin_active conditional I was able to alter the theme to not switch_to_blog if the regional plugin was enabled.

if (is_plugin_active('regional-functionality-plugin.php')) {
	//plugin is activated! Look no switch_to_blog!!
	$args = array(
		'container'    => 'div',
		'menu'         => 'Footer',
		'container_id' => 'footer-links',
		'menu_class'   => 'menu', 
		'echo'         => true,
		'fallback_cb'  => 'wp_page_menu',
		'depth'        => 0
	);
	wp_nav_menu($args);
} else {
	global $blog_id;
	global $current_blog;
	switch_to_blog(45);
	$args = array(
		'container'    => 'div',
		'menu'         => 'Footer',
		'container_id' => 'footer-links',
		'menu_class'   => 'menu', 
		'echo'         => true,
		'fallback_cb'  => 'wp_page_menu',
		'depth'        => 0
	);
	wp_nav_menu($args);
	restore_current_blog();
} ?>

There we have it, 12 lines of code for the love of a single maintainable theme. So, if you want to earn your salt as a WordPress Developer, learn your functions and save your energy for the creative work.

Special guest post by Peter Shackelford

3 responses

  1. That’s pure genius, thanks for sharing.

  2. Peter great work here. i was wondering if I could get some information on any upcoming wordpress meetups. I am from Jackson and would love to gain any knowledge that I can.

  3. JTPratt Media

    This is worth commenting on alone just for the phrase “donkey work” (lol). In all reality – great points though….can’t count the number of times we had to do extra work (costing the client $$) because of a badly coded or poorly executed theme.

Comments are closed for this post. Contact us with any critical information.
© 2009–2024 Digging Into WordPress Powered by WordPress Monzilla Media shapeSpace