class_exists

You can use class_exists to check if a plugin is active. ( Watch the video below which tests the code examples in this tutorial ).

You need to do this when using a PHP class from a plugin in your theme.

If you don’t use class_exists when using plugin functions in your theme, you’ll get a error when you load pages on the site if the plugin is deactivated.

Lets look at a working example using code from a very popular WordPress plugin, Advanced Custom Fields.

Using class_exists In Your Theme

The following code checks if the ACF plugin is active.

Use this code with code snippets which use ACF specific functions like get_field();

If you don’t, your code will cause errors if added to a themes file when the ACF plugin is not active.

The following code checks if the ACF plugin is active by looking for the acf class. If the acf class does not exist the return statement immediately ends execution of the current function.

See the full function which includes class_exists under the sub heading below: The Fix.

if ( ! class_exists( 'acf' ) ) 
    return;

Here’s a complete working example you can add to your child themes functions.php and test yourself which you can see done in the following video demonstration.

Video Demo

The video demonstrates the use of the class_exists function in genesis when using plugin specific functions in a theme to avoid theme errors when the plugin is deactivated.

Code Examples

The following code will work fine if you have created a field in ACF named after_header and activated the plugin.

add_action( 'genesis_before_loop', 'test_acf_function' );

function test_acf_function() {
    
    echo get_field( 'after_header' );

}

However, what happens when you deactivate the ACF plugin?

Fatal error: Call to undefined function get_field()

You get a fatal error because get_field is a function coded within the ACF files so it won’t work when used in a theme when the ACF plugin is inactive.

The Fix

Always add a check for the plugin when using plugin functions in your theme.

Here’s the updated code which include, you guessed it, class_exists.

add_action( 'genesis_before_loop', 'test_acf_function' );

function test_acf_function() {

if ( ! class_exists( 'acf' ) ) 
    return;
    
    echo get_field( 'after_header' );

}

The above code checks if the class_exists for the ACF plugin otherwise returns false

Other Coding Methods

Another method using a ternary operator:

add_action( 'genesis_before_loop', 'test_acf_function' );

function test_acf_function() { 
    
$output = class_exists( 'acf' ) ? get_field( 'after_header' ) : get_post_meta( get_the_ID(), 'after_header', true );
    
echo $output;

}

The above code checks if the ‘acf’ class exists which is true when the ACF plugin is active.

If the ACF plugin is active, the $output variable uses the ACF function get_field( 'after_header' ) as its value.

Otherwise, the $output variable equals get_post_meta( get_the_ID(), 'after_header', true ); which is a WordPress function for custom fields and always works in any theme.

Other Examples

If you’re using WooCommerce functions in your theme, you’ll also want to check that the WooCommerce plugin is active like this:

if ( ! class_exists( 'WooCommerce' ) )
    return;

You can see the above code in many of the new StudioPress child themes which are coded to work with the WooCommerce plugin out of the box and include a dedicated woocommerce folder.

Join 5000+ Followers

Get The Latest Free & Premium Tutorials Delivered The Second They’re Published.