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.
This function is pretty useful if you want to get some data from the wp_posts table, without querying for the whole post.
get_post_field( $field, $post_id, $context );
It speaks for itself.
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!
wp_list_pluck( $list, $field );
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!
antispambot( $emailaddy, $mailto );
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
<input type='checkbox' name='options[postlink]' value='1' <?php if ( $foo == 1 ) echo 'checked="checked"'; ?> />
Using the WordPress checked function
<input type="checkbox" name="options[postlink]" value="1" <?php checked( $foo, 1 ); ?> />
Using standard if statements
<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>
Using the WordPress selected function
<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>
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.
<body <?php body_class(); ?> >
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).
human_time_diff( $from, $to );
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!
wp_send_json_success( $data );
wp_send_json_error( $data );
These helper functions make it pretty easy to pull/post data from/to remote sources, with limited effort.
You’d usually write something like
$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);
But now all you need to write is
wp_remote_get( $url, $args );
Also see wp_remote_post
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”.
get_template_part( 'loop', 'index' );
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!