Unit Tests in WordPress: Making It Work on Travis

Now that we have configured succesfully our environment to run PHP and JavaScript unit tests, it’s time to automate the testing process. Travis CI is a service that allow you to automatically run your tests remotely. If you work alone, you can also avoid using it, thought I would use it anyway for the advantages that it brings. But if you work in a team, even in a team of two, you cannot afford not to use it. Let me explain in a few words how it works. First of all, you must have your plugin or theme on Github. Travis will monitor your Github repository and when it notices that the code has been updated, it downloads the plugin and runs all the tests against the new version of the plugin. In case all the tests succeed you won’t even know. In case any of your tests fails you will be notified by email. The first advantage that I see in this system is that you don’t have to run your tests manually every time that you modify your code. That alone is a huge time saver. To add to that, the whole testing process is simplified if you work in a team. If your partner modifies the code and pushes the modification to Github, the system will run the tests without you having to lift a finger. So yes, automation has a lot of advantages.

Read more Unit Tests in WordPress: Making It Work on Travis

Unit Tests in WordPress: Testing JavaScript With Karma

If you decide that you need to make unit tests on your plugin, it is because it’s quite complex and it’s probably a mix of PHP and JavaScript code. If you think that testing just your PHP code is enough, you are definitely on the wrong path. The JavaScript code can be as complex as the PHP counterpart, and as likely to have bugs in it.

Read more Unit Tests in WordPress: Testing JavaScript With Karma

Unit Tests in WordPress: Installing PHPUnit

PHPunit is a unit test framework for PHP that will be used as a base for testing our PHP code under WordPress. I’ve found three guides on the web that helped me understanding how to install and configure PHPunit. It hasn’t been easy to understand the process because they use different approaches and methods, so you easily get confused.

Read more Unit Tests in WordPress: Installing PHPUnit

Unit Tests in WordPress: An Introduction

This is the first of a series of articles that I’ll make about unit tests in WordPress. I will celebrate my return to blogging with a topic that is hardly (if at all) covered online. Well to be honest there are a few great tutorials about unit testing out there that I’ve used for my personal research but I haven’t been able to find any information about testing together PHP and JavaScript, which is what I’ve accomplished at the end of my research.
Read more Unit Tests in WordPress: An Introduction

Add a Sidebar in Two Steps

Adding a sidebar to WordPress is quite fast and easy, and it usually requires more work to style and position it than to technically add it to the theme. If you need to add a sidebar to a specific page or section of the website you need to know how the template hierarchy works. If you are not sure of which template file WordPress is using for a certain page or your theme has customized the template hierarchy then a plugin like What The File can be of great help.
Read more Add a Sidebar in Two Steps

Problems with jQuery? Make sure you upload the right library

Today I’ve run into a problem on a client’s site, and I want to share with you how I figured it out.

A couple of weeks ago I wrote a very simple accordion function to make a description appear/disappear inside a download box. Today, my client wrote to me saying that the accordion didn’t work anymore, and the description text was always visible. First thing, I checked with Firebug that the script was correctly added to the head of the page. I always use the plugin Header and Footer to add my scripts to posts and pages, and it always worked but you never know. My first guess was that the script for some reason was added before the jQuery library is loaded. In such case the script would not work because it uses a library that is not yet available. However, that was not the case.

When you find yourself in such situation it’s easy to get frustrated. The secret is to stay calm and analyze the situation. The script was working previously, so it could not be a bug in the script itself. After some research I tried to see if the Firebug console was giving me any error. And here I started to understand. I got an error saying TypeError: $(…).accordion is not a function. So, that was definitely a problem with the jQuery library. It didn’t take long to figure it out. The accordion is a function of jQuery UI which is a separate library from the standard jQuery library. The WordPress theme that my client was using uploaded the jQuery library but not the jQuery UI library, so the accordion didn’t work.

To fix that I simply added the two following lines to all the head sections of pages and posts through my Header and Footer plugin:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.4/jquery-ui.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.4/i18n/jquery-ui-i18n.min.js"></script>

So, why did it work before? I think that for a lucky case some plugin loaded the library. The client disabled a few plugins in the last two days and that broke my script.

Lesson learned, always verify that you have the right libraries loaded before writing any script, don’t assume that they are there. It will save you a lot of frustration and debugging hours!

Introduction to ‘The Loop’

Sooner or later it had to come the moment of talking about the WordPress loop. The Loop is one of the core functions in WordPress, probably the function that makes WordPress so different compared to any other CMS.

The Loop displays posts, pages and custom posts. You simply cannot write code for WordPress if you don’t understand how to use the Loop.

Read more Introduction to ‘The Loop’

How to Create Your Template Hierarchy in WordPress

When I decide to learn a specific topic I like to dig deep into it, and understand as much as possible about that topic. Simply learning how the standard template hierarchy works wasn’t enough for me. My problem now was that I wanted to be able to create my personal template hierarchy. After having seen how flexible WordPress is I was sure that it’s possible to do it, and in fact I was right. I’ve found the right hook to understand this topic in a talk given by Kostantin Kovshenin at a WordCamp in Sofia (yes, it was in English, and no, I don’t talk Bulgarian). I watched the video last week, and I was fascinated by the power and simplicity of the mechanism. You can literally turn WordPress upside down. The whole talk includes also an explanation of how to use a filter to alter the WordPress hierarchy but since I haven’t discussed yet filters and actions I’ll let this for another article. Read more How to Create Your Template Hierarchy in WordPress

Understanding The Template Hierarchy (part 2)

In the last article about the template hierarchy I probably forgot to cover an important point, so I’m going to amend now and clarify a few things. I analyzed what happens when someone access the home page of a WordPress website, what templates WordPress looks for and loads first. Now I’ll go deeper into the templates that determine how the home page will look like. Read more Understanding The Template Hierarchy (part 2)

Understanding The Template Hierarchy (part 1)

In this series of articles I’ll try to analyse the structure of the template hierarchy and how WrodPress manages it. I will also try to go deeper and look at the WordPress functions that allow you to completely change that hierarchy to your like. As always, this is the analysis of a beginner learner, so I hope you’ll forgive me if I make some mistakes. Read more Understanding The Template Hierarchy (part 1)