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”.

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

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 the Register Helper Add On. The function this code relies on (pmprorh_add_registration_field()) is defined by that plugin.

function my_pmprorh_init() {
    $field = new PMProRH_Field(
        'company',
        'text',
        array(
            'size' => 40,
            'profile' => true,
            'required' => true,
        ) 
    );
 
    pmprorh_add_registration_field(
        'checkout_boxes',
        $field
    );
}
add_action( 'init', 'my_pmprorh_init' );

It is important to also note that even if you have the Register Helper Add On plugin activated, WordPress will temporarily deactivate it during updates. The customization plugin above runs the pmpro_add_registration_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( 'pmprorh_add_registration_field' ) ) {
        return;
    }
     
    // continue the rest of your function code

    This check will stop the function from executing any code if the pmprorh_add_registration_field function is not available, and this is exactly what we do in our code examples.


  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 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.


  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' );

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

Tagged . Bookmark the . Last updated: .