Easier way to make sure all strings are escaped in WordPress themes

We all know we should escape all the strings in a theme. WordPress theme review guidelines require it, and so does Themeforest.

After working on a theme for a longer period of time, it’s quite possible that you’ve slipped somewhere with unescaped echoes. If you’re going commando on your own – you still need to escape every little thing and be twice as careful if there are no additional set of eyes on your code. More

Starting to write again

We finally launched our website – Colormelon.

For too long we’ve been the shoemakers with no shoes. It was very difficult to find the time to design our own site – it seems that there are always better things to do!
Even right now, there is a huge pile of things we’d rather create than our website, but after years of making web designs for others, we decided that it was finally time we got a website too, or at least start the process of building our site. More

Find all unescaped i18n strings in in WordPress

It turns out, that from now on, it’s a best practice to escape with esc_html__() instead of simply doing __() in your plugins and themes.

Replacing everything with esc_html() is a solution, but what about the __() in your code that already contain some minor code ( like a few wrapping spans here and there ) ? More

Download a complete single page with wget

A simple way to download a complete page

wget -NmkEpnp -e robots=off [URL]

Very much inspired by Guy Rutenberg,
I only modified the snippet slightly with -N, which validates timestamps and doesn’t download duplicated ( but does overwrite local files with the new changes ) and robots=off, so I wouldn’t download the robots.txt

A simple one-liner to convert all JavaScript to CoffeeScript

A simple one-liner to convert all JavaScript in a folder to CoffeeScript

First, make sure you have installed “js2coffee” ( and js2coffee.org is real cool for single files too ):

npm install js2coffee

And then just paste this:

for f in *.js; do js2coffee -it $f > ${f%%.*}.coffee; rm $f; done;

`-it` options modifies the spaces to tabs, remove it if you prefer spaces.


Picking values from an Array in WordPress

_.pick is great

Underscore.js has a lot of great stuff packed in it. One of the functions I love is pick, which lets you input some keys and get a new array from an Array.

Can’t pick in PHP

So today I wanted to find an alternative to Underscore’s _.pick for PHP, and I didn’t. It’s not a function that would be difficult to write, but that’s where WordPress comes in.

Can pick in WordPress

People at WordPress thought that it would be a nice helper for them too, so in WordPress 3.1 they added wp_array_slice_assoc.

The naming is a bit long, but the function is awesome anyway and the naming explains the purpose alright. Slice an array associativley ( that’s a big word ).

Here is how it works.

Imagine we have a some array like this one.

 'a name',
    'jane' => 'also a name',
    'doe' => 'a fake last name',
    'duck' => 'an animal',
    'goose' => 'an animal',

In this case the array is a mess, your array should never be a mess, but it might happen. In this case, I know I want only John and Jane in my array, so I can do this:

$persons = wp_array_slice_assoc( $myray, array('john', 'jane') );

Tara. Now I have a new array “$persons” which has only john and jane.

How is this useful ?

Well, there are times where you would write something like this:

$john = $myray['john'];
$jane = $myray['jane'];
$duck = $myray['duck'];
$goose = $myray['goose'];

Which I think of an overkill, especially if you need like 10 variables. Of course you could extract from all of them


But what if the array has like 20 items. What happens when they clash? What if someone else modifies your code? I just don’t feel that blindly using extract is the best development pattern.

Along comes wp_array_slice_assoc()

$keys_to_extract = wp_array_slice_assoc( 
extract( $keys_to_extract );

Or a slightly more readable (based on preference) variant

$keys_to_extract =  array(
extract( wp_array_slice_assoc( $myray, $keys_to_extract ) );

That’s it.

echo $john; // Echoes: "a name"

Final Note

Please, please be careful extracting variables. And when you do, please document them, even if the documentation comes in the form of $keys_to_extract. Recently I had a very difficult time figuring out what is what because the author of a plugin pulled some weird variables out of nowhere and I had to run around var_dump’ing all over the place until I figured out what is what.

Facing Failure

My Theme Was Rejected

Before it was approved.

In fact, we’ve had 2 themes rejected on Themeforest for various reasons and only now will I admit that I was angry. I was mad! Mad at the reviewers, counting clicks they’ve made reviewing my themes. Mad at myself for developing something that isn’t approved. Mad at the universe for putting me in this position ( I could just have found a case full of money instead of all this… ). How much did that help? Zero. It only led to

Burning Out

Self pity. Anger. Frustration. Fear.
I experienced it all, and it seems that the more I felt any of these emotions, the more I got similar ones. I thought I am never going to get out of it.

For good reason.

I quit my job in January with very limited funds in my pocket. Having done that, I had a motivator that basically says “either you’ll learn how to swim or you’ll sink” was pretty motivating, until I had to face rejection. Once it was introduced, it was like having someone at the shore shouting: “Give up! You can’t swim, just let go and drown already”. Not motivating at all. Which caused the (what seemed to be the) endless misery.

Just do it.

I have no idea what did it for me. It may be that I was already expecting that. I wanted to say prepared, but you just can’t be prepared for that sort of thing, and you’re not even going to get what you we’re expecting as well.

So after all the rejection, and having our Acid theme also rejected, I just decided that nothing is going to stop me. I will work on the Acid theme until it is approved.

As it turns out. It’s all that was necessary. I could write a book where I could describe all the advice I’ve accumulated from all the literature I’ve read on the subject, but it all really boils down to the decision not to fail.

By that I mean – If you think you fail, you failed. If you decide not to fail at all, you won’t. Or as Thomas Edison puts it:

I haven’t failed. I’ve just discovered 10’000 ways that won’t work.

It’s a really simple lesson. At least it sounds simple. But there is no way you’ll learn it but through experience.

Snippet: Get the correct page in WordPress

Here is another really quick snippet.
If you have a page where you’re using Query Posts you’re going to have trouble with Pagination.
Wordpress for some reason likes to use page and paged randomly.

If you assign your page as the “front page”, you’re going to have to get_query_var(‘page’), and if you’re going to assign it elsewhere, it’s going to be paged. This is all fine and dandy, at least when you’re in control, but as soon as someone else starts using your theme, you need something more trustworthy, and that’s what this is.