Best Practice: Check if Function Exists When Adding in WordPress Theme

Recently, a user asked us how to check if a function exists when adding the function to WordPress.

The best part about WordPress is its flexibility, including the ability to add your own functions to your website. However, sometimes your custom code may not work because the function it’s referring to does not exist anymore.

In this article, we’ll show you how to easily check if a function exists before adding it to your WordPress theme or a site-specific plugin.

Checking if a function exists in WordPress

Why Add If Function Exists to Your Custom Code

WordPress is mainly written in the PHP programming language. PHP is a server-side programming language that runs on your WordPress hosting provider’s servers.

Because PHP has to finish running before the page is loaded in your visitors’ browsers, there are certain limitations to it. One of those limitations is that if something goes wrong, it could prevent the whole page from loading.

In WordPress, if a missing function prevents the rest of the code from running, then it halts and displays a critical error or fatal error message.

WordPress critical error message

What would make a function suddenly stop working or go missing?

It’s a more common WordPress error than you might think. For example, let’s say one of your WordPress plugins comes with a function that you have added to your theme’s footer area.

Deactivating the plugin will make the function disappear and cause the critical error on your WordPress website.

That being said, let’s take a look at how to easily check if a function exists before executing it in your WordPress theme files.

Checking If a Function Exists in WordPress

Luckily, PHP programming language comes with a built-in method to easily check the existence of a function before executing it.

Let’s say you have a WordPress function that displays the current time with timezone information. Here’s a sample code snippet that you can use to try it.

//display current time with timezone
function wpb_show_timezone() { 
$better_time = current_time('F j, Y g:i a e'); 
echo "

The current time is " . $better_time ."

"; }

To call this function, you’ll need to add the following code anywhere in your WordPress theme where you want to display the current time.

This is how it looked on our testing site.

Function runs normally

Now if the code responsible for executing this function disappears, then the call to the function will break your website.

Critical error in WordPress

Let’s add a check to make sure that this code only runs when the function exists.


In this code, we are using the function_exists() function. This function checks if a function exists and returns True or False.

We then added an if-else condition to take appropriate action depending on the availability of the function.

Now, when the function is no longer available, the code will simply skip it and WordPress would be able to load rest of your website normally.

Missing function skipped

We hope this article helped you learn how to check if function exists in WordPress. You may also want to see our complete WordPress troubleshooting guide or see our cheat sheet for WordPress theme developers.

If you liked this article, then please subscribe to our YouTube Channel for WordPress video tutorials. You can also find us on Twitter and Facebook.