HTML5 Zone is brought to you in partnership with:

Paulund is a website dedicated to writing tutorials and code snippets about Web Development, the main subjects are PHP, Wordpress, jQuery, CSS3 and HTML5. Paul is a DZone MVB and is not an employee of DZone and has posted 130 posts at DZone. You can read more from them at their website. View Full User Profile

Alert Theme Users To Required Plugins

02.07.2013
| 1743 views |
  • submit to reddit

One of things that I've said about premium WordPress themes before is the amount of features that are included in the theme options panel page. Most of these theme options have so many settings which aren't needed, or there is already a well known WordPress plugin that performs this action.

The best example of this is SEO settings, there are premium WordPress themes that allow you to put in SEO settings into your theme options panel. This is marketed at being a great feature of the theme as you don't need install a new WordPress plugin to do this for you. But I feel this isn't a benefit to the theme and it's just added bloat to the theme that the user really shouldn't use.

I'm saying this because people should be using a WordPress plugin to perform this functionality, mainly because if the user changes the theme then they have now lots all the SEO settings for their site. Other benefits is that these massive plugins are continuously being updated adding better functionality to your site.

I understand the benefit of having the SEO settings in the theme options panel as the user only has to go to one place to customise their whole site but I feel we as developers need to provide the best WordPress experience to users. This includes using WordPress best practises and separating functionality and styling. The reason to have functionality in a plugin is so that it will work exactly the same no matter what theme you are using.

An example is if you want to add Google analytics to your website, use a plugin so that if you change your theme you still have this functionality.

The problem that theme developers have is that a user downloads a theme and might not be a developer or used WordPress before and won't understand that they need to download certain plugins to get the best out of their site. Then the user will install the theme without these additional plugins and complain that certain common website functionality is not included, this functionality might of not been included on purpose.

But if you don't include SEO settings in your theme options panel you need to educate the user on what they should be using. There are a few really good SEO plugins for WordPress, all-in-one SEO, headspace2, SEO Ultimate, SEO Rank Reporter, Google XML Sitemaps, or the plugin I use Yoast WordPress SEO plugin. I would recommend the user of the theme to download the plugin Yoast WordPress SEO and use this for all there SEO settings.

Display An Alert Message To Install Certain Plugins

In this tutorial we are going to go over the steps on how theme developers can provide the user with alert messages to tell the user to download certain plugins to increase the functionality of their site.

For this feature we can use two built in features of WordPress, the first is using a function is_plugin_active() and the second is using the action admin_notices.

The function is_plugin_active() does exactly as it's named it will return a boolean value on if the plugin you are searching for is active. All you have to do is pass in the path to the plugin and WordPress will let you know if this plugin is activated. If this check returns false we can then use the action admin_notices to display a message to the user to download this certain plugin.

required_plugins

Add the following to your functions.php file to alert your user to download certain plugins.

add_action('admin_notices', 'showAdminMessages');
function showAdminMessages()
{
	$plugin_messages = array();
	include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
	// Download the Yoast WordPress SEO plugin
	if(!is_plugin_active( 'wordpress-seo/wp-seo.php' ))
	{
		$plugin_messages[] = 'This theme requires you to install the Yoast WordPress SEO plugin, <a href="http://wordpress.org/extend/plugins/wordpress-seo/">download it from here</a>.';
	}
	// Download the Disqus comment system
	if(!is_plugin_active( 'disqus-comment-system/disqus.php' ))
	{
		$plugin_messages[] = 'This theme requires you to install the Disqus comment system plugin, <a href="http://wordpress.org/extend/plugins/disqus-comment-system/">download it from here</a>.';
	}
	// Download the WordPress popular posts plugin
	if(!is_plugin_active( 'wordpress-popular-posts/wordpress-popular-posts.php' ))
	{
		$plugin_messages[] = 'This theme requires you to install the WordPress Popular Post plugin, <a href="http://wordpress.org/extend/plugins/wordpress-popular-posts/">download it from here</a>.';
	}
	if(count($plugin_messages) > 0)
	{
		echo '
<div id="message" class="error">';
			foreach($plugin_messages as $message)
			{
				echo '
<strong>'.$message.'</strong>
';
			}
		echo '</div>
';
	}
}




Published at DZone with permission of Paul Underwood, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)