Last active
March 1, 2023 14:02
-
-
Save woogists/fdf6e519d9c30d98c9878fbcbd3ba92a to your computer and use it in GitHub Desktop.
[Theming Snippets] Query whether WooCommerce is activated
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Check if WooCommerce is activated | |
*/ | |
if ( ! function_exists( 'is_woocommerce_activated' ) ) { | |
function is_woocommerce_activated() { | |
if ( class_exists( 'woocommerce' ) ) { return true; } else { return false; } | |
} | |
} |
-
From this function name, it isn't obvious that this function belong to your current theme. I had a confusion when I saw this in usage because I thought that this function is from WooCommerce itself similar to WP core functions such as is_home() e.t.c. and was wondering hot it possible to use if WC will not be active. I believe that each function in your theme should have theme prefix even if you have a namespace because namespace can be easily overlooked.
-
Right now we are operating through WP() function and I suggest considering if function_exists( 'WC' ) is a more accurate way to indicate that WC is up and running.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
class_exists
returns boolean, there's no need for IF structure