15 January 2024

15 WordPress Functions Every Developer Should Absolutely Know

By Ronald Smith

WordPress is a treasure trove of incredibly useful functions that us developers can tap into. We can work our magic, conjuring up post lists out of nowhere, manipulating them to our heart’s content, and effortlessly fetching any user we desire, showcasing their social media connections in the blink of an eye.

But, believe it or not, there are quite a few functions that often slip under the radar for reasons unknown. As someone who’s been knee-deep in WordPress coding for a solid 8 years, I still stumble upon hidden gems every now and then! So, let’s dive right in and explore some of these underrated functions that I’ve come to adore, while also learning how to harness their power along the way.


When I tell you about this coding gem, it might surprise you. It’s one of those well-kept secrets that make people raise their eyebrows. The antispambot() function may not be well-known, but it does something important. It helps protect email addresses from being found by sneaky robot scrapers.

Here’s an example of how it works:

$email = ‘[email protected]’;

echo ‘You can contact me at ‘ . antispambot( $email ) . ‘ any time’;

By using this function, you can safely display your email address without worrying about it getting intercepted.

But you know what? This function is a perfect example of why people criticize WordPress. Just look at the name – it’s not exactly obvious what it does, is it? I mean, “antispambot”? That doesn’t tell you much.

Now, let me introduce you to another hidden WordPress gem – the human_time_diff() function. When I discovered this function about a year ago, I thought it was something new. But as it turns out, it had been around for a while.

This function is handy for displaying time differences in a human-friendly way. Instead of showing something like “153 hours ago,” it will display “6 days ago.” Much easier to understand, right?

WordPress has all sorts of little surprises like these. They may be hidden in the codebase, but once you find them, they can make your life a whole lot easier.

Hey there! I’ve got this really cool function that can help me calculate the time difference between two timestamps. It’s been around since version 1.5, which came out on February 17, 2018. Can you believe it?

I found this amazing snippet in the codex that shows how long ago a post was published. It’s super handy! It takes the publish date of the post as the first argument and the current date as the second.

echo ‘This post was published ‘ . human_time_diff( get_the_time( ‘U’ ), current_time( ‘timestamp’ ) ) . ‘ ago’;


Now, let me tell you about this function called “get_post_meta()”. It might sound familiar, but not many people know how it works. Guess what? You can use it to pull all the metadata for a post just by omitting the second and third parameters. How cool is that?

$all_meta = get_post_meta( 14 );

Even if you only need the data for one specific key, it’ll still pull all the postmeta. Pretty neat, huh?

You might be wondering why we use metadata in multiple places for querying the database. Well, it actually makes a lot of sense! If we were to query the database each time we use the “get_post_meta()” function, we would end up with way too many queries. Instead, what happens is that when you pull metadata, it caches all the values and uses them for all subsequent metadata retrievals.

Let me tell you about another handy function called “wp_upload_bits()”. This function is a straightforward way to upload files. It doesn’t move the file to the uploads folder or add it to the WordPress media section, but it’s super convenient. You can always handle the rest with the “wp_insert_attachment()” function.

Check out this example to see how it works:

$upload = wp_upload_bits( $file[‘name’], null, file_get_contents( $file[‘tmp_name’] ) );

You can use this code snippet to upload a file and get all the necessary bits. Simple, isn’t it?

Let me explain something to you: the first thing I want is the name of the file. The second thing, which is so annoying, is that it’s useless, so it should be set to null. The third thing is the actual content of the file.


In the past, I’ve seen people go through all sorts of trouble to get the number of comments for a post. They would write loops or even dedicate entire database queries just for that. But guess what? You don’t need any of that! All you need is this nifty little function called get_post_field(). It does all the work for you by retrieving the value of a specific field for a single post in the database. So, let’s go ahead and grab the comment count!

This post has comments.


I’m going to introduce you to a couple of useful functions that you might not be familiar with yet. They can help you enhance the appearance of your website in a simple and effective way.

The first function is similar to one you might already know called “nl2br.” It’s a function that comes with PHP and it helps you create new lines in your text. But the function I want to show you today goes a step further. Instead of just creating new lines, it wraps your text in paragraphs.

This function is especially useful when you have a textbox and you want to make sure that when users create paragraphs with double line breaks, those paragraphs remain visible on the front-end of your website.

Let me show you an example of how it works:


What Our Users Say


As you can see, the `wpautop` function takes the `$user_comment` variable as input and automatically wraps it in paragraphs. This way, the user’s comments will be displayed in a neat and organized way, making it easier for others to read and follow along.

Now, let’s move on to the second function I want to introduce you to. It’s called `wp_is_mobile()`. As the name suggests, it helps you detect if a user is accessing your website from a mobile device. This can be very handy because it allows you to display content specifically tailored for mobile users.

Here’s an example of how you can use this function:


Visit our website on your desktop for a richer user experience


In this example, the `wp_is_mobile()` function is used in a conditional statement. If the function returns true, it means the user is on a mobile device, so the message between the `if` and `endif` tags will be displayed. Otherwise, if the function returns false, the message will be skipped.

This way, you can provide a different user experience for mobile users, making sure your website looks and functions well on small screens.

I hope you find these functions helpful in improving your website. Have fun exploring them and let me know if you have any questions!


Here’s another cool function: wp_redirect(). You can use this instead of the regular header() function in PHP. With the WordPress redirection function, you can set a URL to redirect to, and even choose a status code. It’s really helpful when you need to handle permanent redirects and more.

// If you have a URL that’s no longer in use, you can do a permanent redirect like this

wp_redirect(‘http://website.com/new-url/’, 301);


Have you heard of this function? It’s not as well-known, probably because the WP-PageNavi plugin stole its thunder. Normally, WordPress just puts previous/next links at the bottom of your list of posts. But WP-PageNavi gives you page numbers instead.

You can actually achieve this with some work using the paginate_links() function. It has several options, so I suggest looking at the documentation for more details.

Take a look at the example below from the codex. It demonstrates how you can add paginate_links() to a default loop. However, adding it to custom loops is not much of a challenge.

To get started, you’ll need to include the following code in your theme file:


global $wp_query;

$big = 999999999; // choose an unlikely integer

echo paginate_links( array(

‘base’ => str_replace( $big, ‘%#%’, esc_url( get_pagenum_link( $big ) ) ),

‘format’ => ‘?paged=%#%’,

‘current’ => max( 1, get_query_var(‘paged’) ),

‘total’ => $wp_query->max_num_pages

) );


By using the code provided above, you’ll have the pagination system up and running on your website in no time!

Now let’s talk about another useful function: wp_die().

This function is a useful addition to the die() function in PHP. Instead of displaying plain text, it shows a WordPress-style HTML message. You can use this function to halt PHP execution and provide a custom message, title, and other arguments. For example:

wp_die( “Oops, you don’t have access to the”, “Permission Denied” );

In WordPress version 5.0, a block-based editor called Gutenberg was introduced. The has_block() function helps identify whether the content contains a Gutenberg block, similar to the has_shortcode() function. It returns true if the content contains a block and false if it doesn’t. You can use it like this:

<?php if ( has_block() ) < // Content has a block. >?>

Lastly, we have the wp_set_script_translations() function.

Hey there! So, here’s the deal: WordPress is making some changes and moving a bunch of stuff to JavaScript. And with all these changes, it needs a way to handle translated text in JavaScript that WordPress can understand. Luckily, there’s a nifty function for that! You can use this function to set translated strings in your scripts. Let me show you an example:

wp_enqueue_script( ‘my-script’, plugins_url( ‘js/my-script.js’, __FILE__ ) );wp_set_script_translations( ‘my-script’, ‘mu-text-domain’ );


Now, let’s talk about another cool function in WordPress 5.0. This function is all about registering a new block in the new WordPress editor. With this function, you can make your very own block that will show up in the editor and do its thing. Fancy, right? Check it out:

register_block_type( ‘my-plugin/new-block’, array( ‘title’ => ‘New Block’, ‘icon’ => ‘megaphone’, ‘category’ => ‘widgets’, ‘render_callback’ => ‘render_function_callback’, ) );


This cool feature lets you use PHP to call WordPress REST API endpoints. It’s really handy when you want to retrieve some information from the API and process it in PHP instead of in your web browser.

Here’s how it works:


$request = new WP_REST_Request( ‘GET’, “/wp/v2/posts” );

$request->set_header( ‘X-WP-Nonce’, wp_create_nonce( ‘wp_rest’ ) );

$response = rest_do_request( $request );

$data = 200 === $response->get_status() ? $response->get_data() : [];


Now, let me tell you about another cool function called `rest_preload_api_request()`.

When you’re building a fancy user interface with lots of JavaScript in WordPress, sometimes you need to load some initial data right on the page. Well, this function is here to help you with that!

You can use `rest_preload_api_request()` in combination with `array_reduce` to make it happen. Isn’t that neat?


// Preload common data.

$preload_paths = array(






preload_data = array_reduce( $preload_paths, ‘rest_preload_api_request’, array() );

wp_add_inline_script( ‘wp-api-fetch’, sprintf( ‘wp.apiFetch.use( wp.apiFetch.createPreloadingMiddleware( %s ) );’, wp_json_encode( $preload_data ) ), ‘after’ );

A Few Hidden Gems

There are some functions that not many people know about, but they can really come in handy. Every couple of months, I stumble upon a new amazing function, and I’m sure my fellow developers out there have even more surprises up their sleeves.

If you have a favorite function that’s not widely known or a function that you think should exist but doesn’t, please share it with us in the comments!


There are certain functions that many people don’t know about. These functions can be extremely useful, but they are often hidden away. I, too, continue to discover new functions regularly, usually about one every two months. It’s fascinating to see how many great functions are out there, waiting to be found. I bet my developer friends have even more secrets to share. If you have a favorite hidden function that you think others should know about, or if you’ve come across a function that you wish existed but doesn’t, please tell us about it in the comments below. We would love to hear from you!