Tips and Tricks

SOLUTION WordPress Fatal error: Call to undefined function

While working with WordPress, at one point of time or the other, you may have come across error message that says: “Fatal error: Call to undefined function … “. In this message, the undefined function could be just about anything such as feed_content_type or get_adjacent_post or wp_get_recent_posts, etc. So how can we solve this? This is exactly what this article is all about. I am going to share a very easy way that you can use in your theme or plugin to gracefully solve and prevent fatal errors from showing up and breaking the normal functionality of your WordPress theme or plugin.

How To Solve Fatal error: Call to undefined function … in WordPress Themes & Plugins

Now let’s say that you are working on a WordPress theme. You would like to use the function “the_excerpt“. Now you do not know whether this function has already been declared or not. So to be on the safer side do the following:

Use the following code in your theme to prevent fatal errors:

The above code will first check to see if the function really exists. If the function is already defined and available for use, the excerpt will be fetched and shown on the screen. If not, it will simply not do anything at all. You can also combine the above code with other conditional code to display another result if the function does not exist. Consider the following example:

You can do the same thing as above in your plugins as well.

How to apply the above code to any custom functions in your custom Plugin and prevent fatal errors

Let’s say you are coding custom plugin. And now you have defined some function such as “my_function1”,  “my_function2”. Now you want to use these in your plugin and want to avoid showing fatal errors. Simply apply the function_exists function prior to using it and that will solve it. Here are couple of examples:

Example 1:

 Example 2:

 Example 3:

As you can see from the above codes, it is really simple. Simply check if the function exists using the function_exists function and that should do it.

Simple, isn’t it?

Do you know of any other ways to solve the Fatal error Call to undefined function problem in WordPress? Feel free to suggest by commenting below.

Share your thoughts, comment below now!