WordPress Template Modification Tips for Non-Programmers - Robustness

By Angsuman Chakraborty, Gaea News Network
Tuesday, December 27, 2005

Several WordPress plugins ask you to add certain code to the WordPress theme template files to make them work. If you are not a PHP developer or you don’t have time to code review the plugin and you decide to activate the plugin then the plugin can very easily cause your site to crash or worse. Often the errors are hard to detect (sporadic, happening only in certain conditions) and even harder to debug as you are not familiar with the code. Today we will talk about a simple step you can take to make your site robust against untested and buggy plugins.

Normally most of the time you are asked to include a code block similar to this:
<?php func_name(arg1,arg2…); ?>
The func_name obviously represents a function which the plugin author wants you to include; the arguments are as required for that function.

This can create two major issues.
Firstly if at any time you decide to disable the plugin then you will have to first remove the code from all your template files before you can safely de-activate / remove the plugin. Otherwise the pages in the site will fail to load properly.

Secondly the plugin itself may fail in certain conditions or always. In the worst case you will find certain pages on your site fails to load sometimes. It could be long before you are aware of the problem.

We will look at two small changes you can make to the code template above to take care of both of the problems described above. First the modified code:

<?php if(function_exists(’func_name‘) @func_name(arg1,arg2…); ?>
Remember to replace func_name with the actual name of the function.

Testing the existence of the function ensures that the code isn’t executed when the plugin is inactive / disabled. This prevents the first problem.

Appending an @ before the function name ensures that errors, if any, while executing the function are ignored and do not cause further problems down the road and do not prevent the overall page from displaying.

This fix works against all versions of WordPress and also in any other templating system which uses php code.

Carefully make the changes following the template above to make your site more robust against WordPress plugins.

January 26, 2010: 10:54 pm

Great information, Thank you so much… keep up the great work.

September 5, 2006: 2:54 pm

[...] WordPress Template Modification Tips for Non-Programmers - Robustness -Simple Thoughts - Java and Web Technology Blog Hoe je wordpress-site bestand maken tegen crashende of niet geactiveerde plugins. Nu het nog overal inbouwen… (tags: howto PHP wordpress plugins) [...]

January 3, 2006: 1:41 pm

[...] Couple that with php’s “silent treatment” of errors. In other words you and your viewers are greeted with a nice looking, white in color web page, whenever any (plugin) error occurs. Plugin errors could be as simple as having blank spaces at the end of plugin files after ?> to more complex ones like invalid argument to methods. Update: Please check tips on making your plugins robust. [...]

December 30, 2005: 12:40 pm

[...] Despite all the warnings above if you still want to upgrade then please please please (repeatation intentional) backup the database and harden your templates against plugins before you upgrade. [...]

December 29, 2005: 1:37 am

[...] Thanks to Simple Thoughts for this simple way to keep things from breaking if and when I have to turn off a plugin in the future. [...]

will not be displayed