When writing a plugin, it’s important to realize one thing about WordPress:
Everyone’s WordPress site is a hot mess.
I’m joking. Kinda.
Seriously though, everybody uses WordPress differently, so you need to account for that.
Avoiding Errors With Plugin Activation
If you are writing a plugin that extends another plugin, you need to use one of these.
For example, let’s say we are extending Advanced Custom Fields. Use function_exists with the name of a function you know you need before you execute any code:
if( !function_exists('get_fields') ) return; // do stuff with ACF here
I like function_exists because it gets right to the heart of the matter. You can also use class_exists, but it’s possible a class could exist but a function could not. (Edge case, but it could happen)
if( !class_exists('ACF') ) return; // do ACF stuff here
Either method makes the code bail if ACF is not active.
You’d be surprised how often this can come in handy. Even if a site has ACF installed, what if it was deactivated by accident?
What if you are writing code that executes too early, and the function you need is not available?
Using these checks makes sure you don’t white screen someone’s site.