Trusted WordPress tutorials, when you need them most.
Beginner’s Guide to WordPress
WPB Cup
25 Million+
Websites using our plugins
16+
Years of WordPress experience
3000+
WordPress tutorials
by experts

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

One of our more technical readers recently asked us how to verify the existence of a PHP function before integrating new code into a WordPress site. This is important to ensure your site runs smoothly and avoids errors.

One of WordPress’s strengths is its flexibility and the WPBeginner team regularly adds custom functions to improve our site’s functionality. We understand that custom PHP code can fail if it calls a function that no longer exists or hasn’t been defined yet.

In this article, we will show you how to easily check if a function exists before adding it to your WordPress theme, allowing you to avoid potential errors in your code.

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 code 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 can 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 header or 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, the 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.

You can add it directly to your theme’s functions.php file, but in this tutorial, we’ll use the WPCode code snippet plugin because it’s the safest and easiest way to add custom code in WordPress.

First, you need to install and activate the WPCode Free Plugin. If you need help doing this, then please see our step-by-step beginner’s guide on how to install a WordPress plugin.

Upon activation, you need to go to Code Snippets » + Add Snippet on your WordPress dashboard. Once there, hover your mouse over the ‘Add Your Custom Code (New Snippet)’ option and then click the ‘Use snippet’ button that appears.

Adding Custom Code in WPCode

This will open a new page where you can add your custom snippet. Type in a title such as ‘Display Current Time With Timezone’ and then paste the code snippet below into the Code Preview pane.

//display current time with timezone
function wpb_show_timezone() {
$better_time = current_time('F j, Y g:i a e');
echo "<p>The current time is " . $better_time ."</p>";
}
Paste the Code Snippet Into WPCode

After that, you need to select ‘PHP Snippet’ from the Code Type drop-down to make sure the code runs correctly.

Finally, you should switch the Activate toggle to the On position and click the ‘Save Snippet’ button to store your new code snippet.

Activate and Save the Code Snippet

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

<?php wpb_show_timezone(); ?>

This is how it looked on our testing website.

Function runs normally

Now, what would happen if the code responsible for executing this function disappears?

The call to the function will break your website like this.

Critical error in WordPress

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

Again, you will need to add the code directly to your theme’s functions.php file or use a code snippet plugin such as WPCode (recommended):

<?php
if( function_exists('wpb_show_timezone')) {
wpb_show_timezone();
} else {
// do nothing
}
?>

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 will be able to load the rest of your website normally.

Missing function skipped

We hope this article helped you learn how to check if a function exists in WordPress. You may also want to see our guide on how to easily display code on your WordPress site or our expert pick of the best code editors for Mac and Windows for editing WordPress files.

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.

Disclosure: Our content is reader-supported. This means if you click on some of our links, then we may earn a commission. See how WPBeginner is funded, why it matters, and how you can support us. Here's our editorial process.

Editorial Staff

Editorial Staff at WPBeginner is a team of WordPress experts led by Syed Balkhi with over 16 years of experience in WordPress, Web Hosting, eCommerce, SEO, and Marketing. Started in 2009, WPBeginner is now the largest free WordPress resource site in the industry and is often referred to as the Wikipedia for WordPress.

The Ultimate WordPress Toolkit

Get FREE access to our toolkit - a collection of WordPress related products and resources that every professional should have!

Reader Interactions

7 CommentsLeave a Reply

  1. kzain

    Saved me a headache! I can’t tell you how many times I’ve added code only to have my theme crash because a function wasn’t there. super easy to implement and keeps my code clean.

    Thanks for the clear explanation

  2. Jiří Vaněk

    I would like to ask, if PHP gives me an error about a non-existent PHP function, can such a function be enabled, or does it have to be done by the server administrator? And is it possible that the function depends on the PHP version? That is, that a certain function is available in one version of PHP but not in another?

    • WPBeginner Support

      In that case the function does not exist so you would need to look into what the specific function is to help find what is causing the error, After that you can either check with the support for what is causing the error or remove what is causing the error.

      Admin

  3. Rakib

    really helpful

  4. Joana Pereira

    Good call Kovshenin. I know exactly what you mean because I was using a custom function with contact form 7 (to retrieve the ip address on each form) and every time the plugin was updated, the theme broke.

    Thanks for the tip

    Joana Pereira

    • kzain

      been there with plugin updates breaking themes too! Using function_exists() is definitely a game-changer for preventing those headaches

  5. kovshenin

    Right, only please stop checking for dynamic_sidebar with function_exists in WordPress themes, unless you need to support WordPress 2.2 and below, which I *highly* doubt. Also with the user photo plugin, the whole approach seems to be wrong and redundant to me, it looks like many developers are missing the whole point of pluggable functions…. Oh well :)

Leave A Reply

Thanks for choosing to leave a comment. Please keep in mind that all comments are moderated according to our comment policy, and your email address will NOT be published. Please Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.