WordPress Template Modification Tips for Non-Programmers - RobustnessBy 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.