WordPress, common mistake while creating a theme

Google ads

  1. Common mistake while creating a wordpress theme

    In the company i worked for, we have created a wordpress plugin for one of our products, my job among other things is to provide a support for plugin we made.

    We got a lot of support tickets saying that our plugin is not working, and after the examination of the problem i see that theme is not coded properly. Since that is a common error i encountered, i have decided to write this article - maybe someone will find it usefull.

    The problem is that many theme coders does not read the instructions on wordpress website. They just skip the wp_head() function and hardcode their scripts which is a very wrong way of doing things.

    On the wordpress site, there is an explanation like this:

    Always have wp_head() just before the closing tag of your theme, or you will break many plugins, which generally use this hook to add elements to such as styles, scripts, and meta tags.

    As it is said above, wp_head() is important for plugins that uses scripts and styles, most common function that plugins use is wp_enqueue_script(). wp_enqueue_script() makes sure that script is included only once, for example, if your plugin needs to have jquery included, you will call wp_enqueue_script('jquery') and if some other plugin do the same, jquery will be included only once, that is the beauty of that function because multiple calls to jquery may break many jquery plugins that are included on page.

    For example, if you have something like this:


    your jquery lightbox plugin will not work and let me explain why.

    You have included jquery.js, and by that, jQuery object is created. By including jquery.lightbox.js, you have extended the jQuery object with the lightbox you also included. Now if you include jquery.js again, you will overwrite the previously created jQuery object and all its extendess. That is why it is important to use wp_enqueue_script() to include your scripts. wp_enqueue_script() function is not reserved just for plugins, it must also be used in themes.

    How to use wp_enqueue_script() function in your theme?

    If you are using some of the pre registered wordpress scripts it all comes to call wp_enqueue_script() function and pass it the name of the script you need.


    List of all wordpress pre registered scripts can be found here.

    If you want to use your custom script with this function, you need to register that script first.

    	wp_register_script('jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js');

    Now, as you may see, if the version of wordpress default jquery script does not fit you, you can alwas register a new one, and above is the example of how to do that.

Google ads