The dreaded undefined function
error is a common PHP error that comes up when developing in PHP. We see it specifically come up when using custom code gists from this site.
This post will describe why this error comes up, how to debug it, troubleshooting a whitescreen, and how to avoid the error when writing code of your own. You’ll learn how to test if a certain function or plugin is active before using that function or a feature of that plugin.
The Undefined Function Error
On live sites, the error will look something like the message below. This error might be hidden and/or logged to a file on the server. If you have not enabled WP_DEBUG, you may simply see a blank screen, something developers call the “white screen of death”.
Turning on WP_DEBUG
will reveal more information about why your site is showing a blank white screen. You may also be able to find exactly where the error occurs by using your browser’s Developer Tools, Inspector or viewing the page source.
Why the Undefined Function Error Happens
Put simply, PHP will throw this fatal error if you attempt to use a function that doesn’t exist. It either hadn’t been defined yet or is part of a theme or other plugin that is inactive or no longer used.
As an example, here is some code that is used to add extra profile fields for members and would be placed in a plugin for PMPro Customizations. This code will trigger the undefined error if you deactivate Paid Memberships Pro. This code relies on the PMPro_Field
class, the pmpro_add_field_group()
function, and the pmpro_add_user_field()
function, all of which are defined by code in the Paid Memberships Pro plugin.
function my_pmpro_add_fields_to_checkout() { $field = new PMPro_Field( 'company', 'text', array( 'size' => 40, 'profile' => true, 'required' => true, ) ); // Add a field group to put our fields into. pmpro_add_field_group( 'Business Details' ); pmpro_add_user_field( 'Business Details', $field ); } add_action( 'init', 'my_pmpro_add_fields_to_checkout' );
It is important to also note that even if you have the Paid Memberships Pro plugin activated, WordPress will temporarily deactivate it during updates. The customization plugin above runs the pmpro_add_user_field()
function during init
. Your site will crash with a fatal error: undefined function
error when the plugin is temporarily deactivated during updates.
How to fix or avoid undefined function errors.
The way to avoid undefined function errors is to make sure the function is defined before it gets used. The best way to do this is with the function_exists()
PHP function.
1. Using function_exists()
At the very top of your custom function, add code like this:
if ( ! function_exists( 'pmpro_add_user_field' ) ) {
return;
}
// continue the rest of your function code
2. Return the Expected Value
If your function expects a return parameter, you’ll have to return the value expected even if the function check is false. Some functions (e.g. callback methods for WordPress filters) expect a certain return value. Here is how you would do that if you were filtering on the the_content
hook:
function my_the_content_filter( $content ) {
// Bail if PMPro is not active.
if ( ! function_exists( 'pmpro_has_membership_access' ) ) {
return $content; //return the original content
}
//... the rest of your code to filter the_content ...
return $content;
}
3. When to use function_exists()
When using plugin functions in a theme file or template, use function_exists()
right before the function is called. In the example above, we used the function_exists()
call at the top of a function as a test to see if a plugin is activated before executing the rest of the function. Similarly, if you are using a plugin’s function in your theme’s files or templates, you should use a function_exists()
test right before the call just in case that plugin is not activated.
For example, here is how we recommend using the pmpro_hasMembershipLevel() function in your theme code:
if ( function_exists( 'pmpro_hasMembershipLevel' ) && pmpro_hasMembershipLevel() ) {
//this is a member, do something cool
}
4. Other ways to check if a plugin is loaded
Besides checking if a functions exists, you can also check if a class exists, a method exists for a certain class, or a constant is defined. Here are a couple other ways to test if PMPro is activated.
class_exists()
if ( class_exists( 'MemberOrder' ) ) {
//do something with the MemberOrder class, e.g. $order = new MemberOrder();
}
method_exists()
if ( method_exists( 'MemberOrder', 'getEmptyMemberOrder' ) ) {
//getEmptyMemberOrder method added in v1.8.6.8 is available
}
defined()
if ( defined( 'PMPRO_VERSION' ) ) {
//PMPro has been loaded
}
5. Avoid Activation Libraries
There is a nifty library you can use to make plugins require other plugins called TGM Plugin Activation. We’ve used this in the past, but have removed it from some Add Ons. Today, we avoid using it because including the library has caused more issues that it solved. If another plugin was using an older version of the TGM library, then there could be conflicts.
By testing for a plugin first (looking for a class, function, or constant), there is no chance for a conflict.
6. When to check if a plugin is loaded.
Make sure your check is always at the top of the function. When editing our gists or other code on this site, make sure that your edits go below any function/plugin tests. So…
function my_function() {
//don't put any code up here
//test for PMPro
if ( ! defined( 'PMPRO_VERSION' ) ) {
return;
}
//put your edits below here
}
7. Special Exception for Plugin Hooks and Callbacks
If you are writing a callback for a hook added by a certain plugin, you don’t have to test if that plugin is active. When you define a callback function via add_action() or add_filter() for a hook or filter added by a specific plugin, you don’t have to check that that plugin is active. The fact that the hook fires at all is proof that the plugin is active. So for instance, the PMPRO_VERSION
check at the top of this callback is redundant and not needed.
function my_pmpro_checkout_level( $level ) {
//this check if redundant because the pmpro_checkout_level filter is only used by PMPro
if ( ! defined( 'PMPRO_VERSION' ) ) {
return $level;
}
//... filters here ...
return $level;
}
add_filter( 'pmpro_checkout_level', 'my_pmpro_checkout_level' );
Navigating Undefined Function Errors with Confidence
Particularly within the context of WordPress hooks, filters, actions, and custom code, it’s important that you understand preventive measures that help you avoid undefined function errors down the road. This post aims to share a few reasons behind these errors, to give you both a foundational understanding and a path to solve errors if they occuer. As a recap, here are the key takeaways:
- Ensure Function Availability: Use the
function_exists()
check to verify the presence of a function before use. - Safeguard Return Values: Always return the expected value, especially when conditional checks prevent a function from executing.
- Timely Checks: Implement
function_exists()
or similar checks immediately before calling a function to avoid unforeseen errors. - Alternative Verifications: Explore other methods like
class_exists()
,method_exists()
, and checking for defined constants to be 100% certain a plugin or feature is available before use. - Placement of Checks: Adding checks at the beginning of your functions saves time by preventing unnecessary code from running.
- Understanding Context: Recognize when checks are unnecessary, such as within callbacks for plugin-specific hooks, to streamline your code.
By understanding and adopting these strategies, you and your developer will significantly reduce the chance of errors in the future. Remember, the key to undefined function problem-solving lies in understanding the root cause. Only then can you can update your code to make the right function check for your project. For additional support, please reach out to our dedicated support team.