Some Useful WordPress Functions

WordPress-Logo

WordPress is full of really well known and useful PHP functions that help developers get their job done quicker – but there are a few lesser known ones that are also extremely helpful to have in your utility belt when making a WordPress site. This article goes over a few of the ones I’ve found recently that I didn’t always know about. Hopefully there will be a couple new ones that you can take advantage of after reading the article.

anitspambot( … )

This function converts email addresses characters to HTML entities to block spam bots. This is actually the function that I found that made me want to do this article (I’ll also be added this to DobsonDev Shortcodes). You can see an example below:

is_email( … )

If you need to check that an email is valid – WordPress has you covered. The is_email( … ) function will check this for you so that you don’t have to mess with any regular expressions.

wp_is_mobile( … )

wp_is_mobile( … ) will check to see if the user is browsing on a mobile device. As sites need to become more and more responsive this function will only increase in importance so be sure to remember it!

Form Helper Functions

WordPress actually has 3 form helper functions built right in. These are checked( … ), selected( … ) and disabled( … ). All three of these functions will check two values and if they are the same they will either check a checkbox (checked( … )), select an option (selected( … )) or disable an input (disabled( … )). Look over the example below and then I’ll explain exactly what happens:

As you can see, we have $value = 1. This means that our checkbox will be checked, the 5th option will be selected and our text input will be disabled. This is because all of these method calls check the value of $value against the value 1. It’s as simple as that!

wp_unique_filename( … )

When you are adding a file to a directory normally you would check to see if the file name is unique so you don’t accidentally overwrite an existing file. Luckily for us we can save some time by using the built in WordPress function wp_unique_filename( … ). This function gets passed a directory and a file name and then the code will check to see if the file name exists in the given directory. The best part is that if a file already with the given name then the function will return the file name with a number appended to the end.

trailingslashit( … )

If you want to ensure that a URL has a trailing slash on the end, the trailingslashit( … ) function will do the trick. If there is no trailing slash it will add one but if there already is one then it will ensure that there is only one. Below is an example of using it in the same way we used wp_unique_filename( … ), but this time I don’t have to check if wp_unique_filename( … ) returns a slash or not because I can use trailingslashit( … ) to ensure one is there.

human_time_diff( … )

The human_time_diff( … ) function will return the difference between two times in human readable format. One of the best uses for this function is to show how old a post is. An example of this can be found below.

wp_pluck_list( … )

The wp_pluck_list( … ) function will pluck a certain field out of each object in a list. We can use this to list all our post titles, ID’s for pages, tags for events, etc… This is an extremely useful function and is often under utilized.

Since WordPress 4.0, wp_pluck_list( … ) takes a third argument that makes it behave more like array_column(). The third argument allows you to specify a key whose value will be used as the key of the returned array. Check it out in action below:

That’s all I have for now, but if I find more functions in the future I will be sure to do a follow up article. As always thank you for reading and please share it around as much as you can! Please feel free to put any suggestions or ideas for future tutorials in the comments section below.