Paulund
2013-02-06 #wordpress

Alert Theme Users To Required Plugins

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.

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 '<p><strong>'.$message.'</strong></p>';
            }

        echo '</div>';
    }
}

TGM Plugin Activation

Another way you can alert your theme users of plugins that they need is to install the TGM plugin with your theme. TGM Plugin Activation is a PHP library that allows you to easily require or recommend plugins for your WordPress themes (and plugins). This library should be included into your WordPress theme, when you have loaded this library you will have access to the action tgmpa_register. This action should be used to define what plugins that you recommend to use with this theme. Perfect if your theme has theme files to use custom post types, then you can create a plugin to setup these custom post types. TGM Plugin