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, and how to avoid this 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 error I’m talking about looks something like this:

Fatal error: Call to undefined function somefunctionname() in /var/www/vhosts/somedomain.com/httpdocs/wp-content/plugins/pmpro-customizations/pmpro-customizations.php on line 163

On live sites, errors like this might be hidden and/or logged to a file on the server, and so you will only see a blank “whitescreen of death”.


Why this error happens.

Put simply, PHP will throw this fatal error if you attempt to use a function that hasn’t been defined yet.

As an example, here is some PHP code that will cause an error like this if the PMPro Register Helper plugin is deactivated since the function pmprorh_add_registration_field() is defined by that plugin.

function my_pmprorh_init()
{
    $field = new PMProRH_Field(
        "company",              // input name, will also be used as meta key
        "text",                 // type of field
        array(
            "size"=>40,         // input size
            "class"=>"company", // custom class
            "profile"=>true,    // show in user profile
            "required"=>true    // make this field required
        ));
 
    pmprorh_add_registration_field(
        "checkout_boxes", // location on checkout page
        $field            // PMProRH_Field object
    );
}
add_action("init", "my_pmprorh_init");

Even if you have the Register Helper Add On activated, it will be temporarily deactivated during updates. The customization plugin above runs the pmpro_add_registration_field() function during init, and so your site will crash with a fatal error:undefined function when the Register Helper Add On gets deactivated during update.


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.


Using function_exists()

At the very top of your function, add some code like this:

if(!function_exists('pmprorh_add_registration_field'))
    return;
 
//rest of your function code

These two lines will stop the function from executing any code below if the pmprorh_add_registration_field is not available, and this is exactly what we do in our register helper example code.


If your function expects a return parameter, you’ll have to return the value expected

Some functions (e.g. callback methods for WP 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;
}

When using plugin functions in a theme file or template, use function_exists() right before the call

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
}

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
}

We avoid using the TGM Plugin Activation library

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 addons and avoid using it now because we found including the library caused more issues that it solved. If another plugin was using an older version of the TGM library, then there could be conflicts. If we simply test for a plugin by looking for a class, function, or constant definition, then there is no chance for a conflict there.


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
}

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_pmpmro_checkout_level($level) {
    //this check if redundant because the pmpro_checkout_level is only used by PMPro
    if(!defined('PMPRO_VERSION'))
        return $level;
 
    //... filters here ...
    return $level;
}
add_filter('pmpro_checkout_level', 'my_pmpro_checkout_level');

Let us know if you found this useful or if you have other questions around this or similar errors.


Comments (9)

Author’s gravatar

I am using the Divi Theme, and I keep getting the error, “Fatal error: Call to undefined function bbp_is_forum() in /home4/spanish/public_html/homeschoolspanishcurriculum.com/wp-content/plugins/pmpro-bbpress/pmpro-bbpress.php on line 63”

Where should I be placing the code to fix this problem. I have only installed the Paid Memberships Pro plugin and the bbPress Add On. I want to purchase the full version, but I want to make sure it works first.

Please advise. Thanks.

Reply
Author’s gravatar

That error might be thrown if bbPress itself wasn’t active. Or if the pmpro_bbp_is_forum() function is used before bbPress is loaded. Maybe the Divi theme makes a call?

Reply
Author’s gravatar

In any case, if you post to our member forums and follow up (can use a private reply) so we can gain access to your site, we can work through this for you and get it working. We’ve fixed other issues with the Divi theme for some other customers. If I knew exactly what was happening, I would just fix it for you, but it seems like we’ll need access to your site to debug.

Reply
Author’s gravatar

How can I do a private Reply? I would like to make sure this works before I purchase the Pro edition…

Author’s gravatar

Final follow up… 🙂 You could fix that error by adding if(!function_exists(‘bbp_is_forum’)) return false; to the top of our function there (and I might), but I can’t see why/what would be calling pmpro_bbp_is_forum too early, which is the real issue. So adding that line would fix the fatal error and the site would run, but it might leave another issue where pmpro_bbp_is_forum() is being checked early and we could be returning false incorrectly here. Hope that makes sense.

Reply
Author’s gravatar

Hi

I see a log error in my Extra Expiration Warning Emails Add On , and I see that my expiration messages do not seem to be firing . can you help me? I know almost nothing of PHP code

LOG:

[14-Sep-2016 15:30:01 America/Sao_Paulo] PHP Fatal error: Call to undefined function add_filter() in /home/wingchu/public_html/wp-content/plugins/pmpro-extra-expiration-warning-emails/pmpro-extra-expiration-warning-emails.php on line 12

Is this the 12 line in the code ?

add_filter(“pmpro_send_expiration_warning_email”, “__return_false”);

Whats Happens?

Thank You

Reply
Author’s gravatar

That’s really odd. I can’t imagine why the plugin would be loaded before the add_filter function is defined in WordPress. Do you have the addon just installed as a normal plugin? Can you open a thread in our member forums so we can help you with this?

Reply

Leave a Reply

Your email address will not be published. Required fields are marked *