0

I'm working on a theme right now and I'm using some functions like is_shop() in the header if the woocommerce plugin is installed. However when the plugin isn't installed these functions aren't defined and I run into some issues. What is the best way to deal with this? I've thought about just defining empty functions like function is_shop() { }. Is this the best way to do this? Or should I do something else?

Thanks.

1 Answer 1

3

If you define your own empty function, then it can cause a problem when the plugin tries to load its same function. The simplest way around this is to use PHP's built-in function_exists() method wrapped around the function call. If that function is defined (like inside the plugin) it'll run the call. If not, it's skips over it without a second thought.

if ( function_exists( 'is_shop' ) ) {
    is_shop();
    // do other stuff related to is_shop()
}
1
  • Ugh, I thought this might be the answer. Need to edit a lot of stuff now.
    – Johnny
    Commented Jun 15, 2015 at 21:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.