How to Customize Theme Check for Validating WordPress Themes

Security and consistency are some of the major concerns we have in using third party libraries, frameworks and plugins. We face the same problem in using free and commercial WordPress themes and plugins.

The WordPress Codex provides a set of guidelines and rules for developing themes. In order to submit themes to the WordPress theme directory, you have to adhere to those guidelines. But there are hundreds of free and commercial themes which might not be developed according to these guidelines.

Basically we have to check whether a theme matches at least the given set of guidelines and rules. Theme Check is a free WordPress plugin developed to validate WordPress themes.


Can Theme Check Validate Everything?

It definitely can't, at least, with its default functionality. So here we are going to see how we can use the existing checks as well as add our own rules for validation of WordPress themes.


Importance of Theme Check Plugin

Theme Check comes up with around 40 built-in tests to validate your theme on both the front-end and admin dashboard. Basically those checks will contain necessary validations for avoiding security concerns and matching theme functionalities against theme development guidelines.

As theme developers or users of third party themes, we should be using such a tool to make sure our themes are up to standards.

The major importance of theme checker comes with its ability to change existing behavior or add new behavior through pluggable plugins. Throughout this article we will be looking at its architecture and the possibilities of extending the plugin to use it as a fully featured theme checking library.


How to Use Theme Check

I assume that most of you have already used this plugin in your WordPress developement tasks. Those who are new to this plugin can grab a copy from the WordPress plugin repository at http://wordpress.org/extend/plugins/theme-check/.

Once installed and activated, you will get a sub menu called Theme Check under the Appearance menu. There you can find a dropdown box containing all the available themes on your WordPress installation. You can select a theme and click the Check it button to start the theme validation. Once the process is completed, you will get a screen like the following.

Screenshot-195
Make sure to enable the WP_DEBUG option in the wp-config.php file to TRUE before you start validating themes.

Theme Check results screen will contain all types of errors found in your theme in different error types such as RECOMMENDED, REQUIRED, WARNING, etc. Keep in mind that all these validations are done based on the WordPress theme review standards.

The actual power of Theme Check comes when we extend the plugin with custom validation checks. So let's dig into the code to find out how it works and how we can extend it with our own checks.


Understanding Theme Check Interface

Theme Check offers a well defined and simple interface for creating checks. All the existing checks as well as new checks need to implemented in this interface to make things work. The following code contained in the checkbase.php file shows the common interface for theme checks.

Basically it contains two functions for checking the theme and providing errors. All the file contents of your theme will be compressed into three main variables as strings. Those variables are then passed into the check function of each check class by the plugin.

Inside the check function we have to implement the validations, and return errors if the check completes as a failure. Then the getError function will be called to add the error messages into the Theme Check results page.


Introduction to Theme Check Process

I think it's important to understand the initialization process of Theme Check in order to find out how it works and the important sections we need to focus, before extending the plugin functionality.

Once we select the theme and click the 'Check it' button, plugin will search for all the files in the selected theme using the PHP RecursiveDirectoryIterator and assigns the whole content of those files into a string variable.

Then the file contents will be separated into three variables, one each for PHP files, CSS files and other files. Then it starts the process of running theme checks, which will be the most important part for us as developers.

Consider the following code for the initial execution process after loading the file variables.

Code Explanation

  • All the theme checks are stored in the checks directory of the plugin and each of them is included by searching for the PHP files in the directory.
  • Once all the theme checks are loaded, the plugin executes a custom action called themecheck_checks_loaded. This action acts as the most important part in the extending process.
  • Then the system starts to execute theme checks through the run_themechecks function, which takes PHP files, CSS files and other files as variables.
  • Finally the check function of each of the theme checks is loaded into the global $themechecks variable which will be executed to complete the checking process.

Customizing Existing Theme Checks

Most of the theme checks are based on matching regular expressions or matching specific positions in strings. Our first requirement is to figure out how we can modify the behavior of existing checks. So let's take a look at the IncludeCheck class, which validates how files should be included into a theme.

Code Explanation

  • All theme checks have a protected array for storing errors.
  • Inside the check function, we can include any number of checks in an array. Here we are only having one check.
  • Most of the theme checks will be executed by matching regular expressions and hence the keys for the array of checks will be regular expressions. The value of the respective key should contain the error message to be displayed in the case of failures.
  • Then we have to choose a specific type of file and traverse through each check in the array while updating the global checkcount variable.
  • Next we conduct the regular expression match and assign the errors to global error array on failures.
  • Afterwards we return the status of the check as a success or a failure. Depending on the status, the plugin will grab the necessary errors to be displayed on the results page.
  • Finally we initialize the object of the theme check class and assign it to the global $themechecks array at the end of the file.

Assume that you want to improve an existing theme check by adding new rules or modifying existing rules. All you have to do is add a new item or change the regular expressions of existing items in the checks array.

Now you should have a clear understanding of how theme checks work and how the errors are generated on failures. Let's move onto the most important part of creating our own custom checks without effecting the core plugin.


Extending Theme Check With Pluggable Plugin

Building new theme checks can be as simple as implementing the interface with a new theme check class and putting the files inside the checks directory of the plugin. But changing the core functionality is not a recommended method as you will always lose your files on plugin updates.

Best practice is to extend plugins using possible options without affecting the core code. Developers of Theme Check have enabled the option of extending functionality using an action hook called themecheck_checks_loaded.

This action hook will be executed after all the theme checks are loaded. Therefore we will have access to the global theme check variable inside pluggable plugins. Now its time to get started by creating a custom plugin.


Removing Existing Checks

Sometimes we might need to disable some theme checks in order to match the requirements of our own themes. You have to either remove the file or comment the initialization code in order to disable them in a normal scenario. But with the power of a pluggable plugin, we can enable or disable them at any time without affecting the core. Let's see how we can disable theme checks using a plugin.

We can implement the themecheck_checks_loaded action of the Theme Check plugin, inside our own plugins. There we have access to all the loaded theme checks. We can define an array to contain the class names of theme checks we want to disable. Finally we unset the checks contained in the disabled array and theme check will be executed without these validations.

We are allowed to easily modify the existing theme checks using our own plugin. Implementing a theme check from scratch is the main intention of this tutorial. So let's see how we can create a new theme check.


Creating New Theme Checks

There can be hundreds of scenarios for implementing custom theme checks. Validating custom action hooks is one of my favorite and I'll be using the code for explanations.

Basically I would like to have certain custom action hooks in my themes to add new functionality. The Thematic theme is a good example for effective usage of custom action hooks in themes. Let's get started on implementing a custom theme check.

Inside the custom plugin folder, create a new PHP class and implement the interface with the details discussed in the previous sections. The easiest way is to copy an existing theme check and change the check function and initialization. Consider the following code for the implementation of checking custom action hooks.

In this custom check, I have included regular expressions to validate two custom action hooks called sample_action1 and sample_action2. After a match is found we define a user friendly key to be displayed to the user instead of the original regular expressions. If you compare it with existing checks, you will notice that regular expressions and messages of the check function are the only things which have been modified.

Finally we can add new checks using another implementation of the action as shown in the following code.

Now you can integrate your own theme checks into the Theme Check plugin.


When to Use Theme Check

Theme Check is developed to validate themes against the WordPress theme development guidelines. Hence it will focus mainly on security concerns and keeping consistent format and features.

We can use this plugin from a different perspective to add features as shown in the last section. If you are an expert in regular expressions, it is possible to create some advanced theme checking with this amazing tool.

It's up to you to decide when to use custom theme checks. Of course, this is more important for theme developers rather than users of WordPress themes.

I can think of two major scenarios where custom theme checks can become useful.

Maintaining a Basic Framework

I don't think any developer will develop each WordPress theme from scratch. Most often developers keep their own basic theme framework and build various designs on top of that.

So custom theme checks will be quite useful to keep the consistency of a basic framework across all themes. You can validate things like image formats, sizes, shortcodes, actions etc. inside your own framework.

Developing Themes Beyond Blogs

Generally most WordPress themes are designed to suit a blogging type of theme. WordPress offers built-in action hooks for blog features and hence we won't have any difficulties extending them.

However, there can be scenarios where you develop themes for applications such as job posting sites, shopping carts, event management systems etc. Those themes have completely different sections and features compared to blogs. WordPress doesn't offer built-in features for such themes. Hence it would be a good idea to implement action hooks inside such themes and validate them using Theme Check.


Conclusion

I hope you learned the importance of Theme Check and how we can use it to keep consistency across our themes.

I would like to know whether you have used this plugin and what kind of custom theme checks you have implemented to extend its features.

Feel free to share your suggestions and questions in the comments section below.

Tags:

Comments

Related Articles