15 Aug 2013

Top ten unknown WordPress functions

So you’re “wired in” working on a WordPress project and you need a function to do something amazing. Chances are that amazing function already exists in the WordPress core.

Doing a quick Google search doesn’t always show the most relevant results for WP functions, however digging into the core is like a gold mine (for nerds). Here’s a list of some of those useful (but often unknown) functions you should be using.

get_post_field

This function is pretty useful if you want to get some data from the wp_posts table, without querying for the whole post.

[code language=”php”]get_post_field( $field, $post_id, $context );[/code]

It speaks for itself.

Codex it!

wp_list_pluck

At first glance it looks to be missing the word chickens, but with that aside, this is an extremely useful function. Let’s say you have an array of objects and you want to simply ‘pluck’ out a certain value from that array (without a loop), and place it into a new array; well, this is that function!

[code language=”php”]wp_list_pluck( $list, $field );[/code]

Codex it!

antispambot

I’m quite surprised how often this isn’t used or is replaced with a third-party library. This function will convert an email into HTML entities to help fight against those pesky spam bots. Simply pass it an email (and an optional mailto parameter) and voila!

[code language=”php”]antispambot( $emailaddy, $mailto );[/code]

Codex it!

checked/selected

You’ve probably seen a fair number of select options with if statements to check if a value is selected or not. Well, WordPress has a function for that (because it’s awesome). These two functions do all the hard-work for you. The example below simply checks if $foo equals 1; if it does, then WordPress will ensure it has the correct attributes.

Using standard if statements

[code language=”html”]<input type=’checkbox’ name=’options[postlink]’ value=’1′ <?php if ( $foo == 1 ) echo ‘checked=”checked”‘; ?> />[/code]

Using the WordPress checked function

[code language=”html”]<input type=”checkbox” name=”options[postlink]” value=”1″ <?php checked( $foo, 1 ); ?> />[/code]

Using standard if statements

[code language=”html”] <select name=”foo”>
<option value=”1″ <?php if ( $foo == 1 ) echo ‘selected=”selected”‘; ?>>1</option>
<option value=”2″ <?php if ( $foo == 2 ) echo ‘selected=”selected”‘; ?>>2</option>
<option value=”3″ <?php if ( $foo == 3 ) echo ‘selected=”selected”‘; ?>>3</option>
</select>
[/code]

Using the WordPress selected function

[code language=”html”] <select name=foo”>
<option value=”1″ <?php selected( $foo, 1 ); ?>>1</option>
<option value=”2″ <?php selected( $foo, 2 ); ?>>2</option>
<option value=”3″ <?php selected( $foo, 3 ); ?>>3</option>
</select>
[/code]

body_class

So before I realised this little gem existed, I wrote a function to automatically add classes to the body tag in themes I was working on, but no more! Not only does this function add pretty much every class you could possibly need, but it also lets you add your own, and it’s filterable! Simply add it within your body tag and job done.

[code language=”php”]<body <?php body_class(); ?> >[/code]

Codex it!

human_time_diff

This is similar to the popular Twitter “time ago” functions; just pass the function two time-stamps to be presented with a human readable time difference. Quite useful for displaying when a post was last modified (e.g. last updated 1 hour 30 mins ago).

[code language=”php”]human_time_diff( $from, $to );[/code]

Codex it!

wp_send_json_success/error

A couple of new functions in WordPress 3.5 which save a bit of time (and code) for returning a JSON object. Basically, this is instead of creating an array, encoding it then echoing it. Simple, but effective!

[code language=”php”] wp_send_json_success( $data );
[/code]

Codex it!

[code language=”php”] wp_send_json_error( $data );
[/code]

Codex it!

wp_remote_get/post

These helper functions make it pretty easy to pull/post data from/to remote sources, with limited effort.

You’d usually write something like

[code language=”php”] $ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$data = curl_exec($ch);
curl_close($ch);
[/code]

But now all you need to write is

[code language=”php”]wp_remote_get( $url, $args );[/code]

Codex it!

Also see wp_remote_post

get_template_part

This function loads a template part into a template. This is pretty useful for reusing parts of a template, and helps to keep your theme files neat (which is always a good thing). It helps group files together within your theme, such as all files used for the “loop”.

[code language=”php”]get_template_part( ‘loop’, ‘index’ );[/code]

Codex it!

Okay, so it wasn’t exactly 10 functions (unless you count the grouped ones), but I bet now you’re itching to use at least one of them!

I don’t know every function (yet 😉 ), so let me know if I’ve missed any, or if you’ve come across a super-duper helpful function!

Happy coding.

The authors

Tomasz Bakula
Developer