Chandra Patel

Just Another Developer

Guidelines for developing plugin or theme for WordPress

Hello friends, here i write some guidelines for developing plugin and theme for WordPress. I inspired to write this article when i am facing some problems when developing plugin or theme for WordPress or using third party WordPress plugins and themes. Below are the some guidelines which i learn myself and share with you.

Notes to keep in mind when you developing plugin or theme for WordPress.

1. function_exists

function_exists() is core php function to determine whether function is exists or not. You can use this function in two condition,

  1. when you calling some function and you don’t know whether that function is exists or not.
    For example, You want to call function_1() then you can use below code.

    if( function_exists( 'function_1' ) ) {
        function_1();
    }
  2. When you are going to defined some function.
    For example, you want to defined function but before that you want to check whether that function is already defined or not then you can do as following.

    if( ! function_exists( 'function_1' ) ) {
        function function_1() {
        }
    }

Using this habit, you can avoid error like fatal error, function already defined error and important thing is site does not break down because of your coding.


2. Always check function dependencies.

Here, what i try to explain is, whenever you use any PHP/WordPress function then check whether this is deprecated or is available in new version of PHP/WordPress. I ‘m explain you with real example.

I am developing one plugin and in this plugin i use add_filter() function and in this function i need to return true so i use anonymous function (PHP) so my code look like this

add_filter( 'billing_address_required', function() {
    return true;
} );

So i am going to save my time using anonymous function. And i am happy to because i achieve using less code. So my code is working fine with php version >=5.3.

And then one day i knew that this is not working in php version 5.2 because this feature is not their in php 5.2 version and i need to rewrite my code again. I am not worried about code rewrite but i am worried about my impression to my client/user who use my plugin in their site. I am giving example of one function but there are lots of function need to check before use it.

So my advise is Always check function dependencies twice.


3. Use unique keyword/prefix when you defined function.

Here is also small incident happened with me. I found that premium plugin and one free plugin is not working together because of fatal error and fatal error is about try to declaring same function name twice.

I found that both plugin defined function called pagination(). That’s why both plugin is not working together. Then i thought why this happened because many people think in same way, may be possible or may be not.

Then i thought why people does not use any unique keyword or say prefix in function declaration. Assume if pagination() function is defined like this plugin1_pagination() and plugin2_pagination() then there is no chance for conflict.

So my advise is always use unique keyword/prefix in your function declaration or you can use function_exists() function (See Point 1 above).


4. Remove PHP warnings and notice errors.

Always write code in such way so there is no chance of generating PHP warnings and notice error. Because this is very bad thing. Always keep debug true and display errors when you are in developing mode.