Guest User

Untitled

a guest
Nov 20th, 2017
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.18 KB | None | 0 0
  1. <?php
  2. /**
  3. * Detect if a WordPress plugin is active
  4. * A function you can use to check if plugin is active/loaded for your plugins/themes
  5. * @link //gist.github.com/llgruff/c5666bfeded5de69b1aa424aa80cc14f
  6. */
  7.  
  8. // When coding plugins that rely on another one, like Private Content for bbPress or Visual Attributes for WooCommerce, you need to make if the WordPress Plugin is active to initialize your plugin routines or display a notice saying that the required plugin must be activated. In this tutorial we’ll see how to detect whether a certain plugin is active in a couple of ways.
  9.  
  10. ## 1. Check whether a certain class or function or constant exists
  11. // While these methods can be used to know if a plugin is activated, they’re also useful for plugins with a large amount of modules like Jetpack or WPML so we can figure out whether a particular module is enabled.
  12.  
  13. # Example. Check if bbPress plugin is loaded
  14. if ( class_exists( 'bbPress' ) ) {
  15. // bbPress is enabled so let's begin
  16. }
  17. # Example. Check if Jetpack's Sharing module is available
  18. if ( function_exists( 'sharing_display' ) ) {
  19. // sharing is enabled, do something
  20. }
  21. # Example. Check if CMB2 is loaded
  22. if ( defined( 'CMB2_LOADED' ) ) {
  23. // code that requires CMB2
  24. }
  25. # Example. Check if WooCommerce is activated
  26. if ( class_exists( 'WooCommerce' ) ) {
  27. // code that requires WooCommerce
  28. }
  29. # Example by plugin author. Check if WooCommerce is active //docs.woocommerce.com/document/create-a-plugin/
  30. if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {
  31. // WooCommerce is active
  32. }
  33.  
  34. ## 2. Using the standard function is_plugin_active
  35. // This function is initially only available in the WP Admin, and as such, code using it must be hooked to admin_init or a later action. It’s possible to use it in the front end but you need to load the file wp-admin/includes/plugin.php with the additional load it implies. Its usage is like
  36.  
  37. # Example. Check if QueryLoop Visual Attributes is active
  38. if ( is_plugin_active( 'ql-visual-attributes/ql-visual-attributes.php' ) ) {
  39. // Visual Attributes is activated
  40. }
  41. # Example. Check if QueryLoop Visual Attributes is active [to use in front end]
  42. include_once ABSPATH . 'wp-admin/includes/plugin.php';
  43. if ( is_plugin_active( 'ql-visual-attributes/ql-visual-attributes.php' ) ) {
  44. // Visual Attributes is activated
  45. }
  46.  
  47. ## 3. Check if the plugin is active with custom function
  48. // While the two first approaches above are good (third one is also good but we have to load that additional file), there might be a case when the required plugin only exposes its classes and functions after a certain moment and they’re are not yet available when your own plugin is first activated. In these cases we can check if the plugin is activated in single site or network wide.
  49.  
  50. /**
  51. * Checks if the required plugin is active in network or single site.
  52. * @param $plugin
  53. * @return bool
  54. */
  55. function main_is_plugin_active( $plugin ) {
  56. $network_active = false;
  57. if ( is_multisite() ) {
  58. $plugins = get_site_option( 'active_sitewide_plugins' );
  59. if ( isset( $plugins[$plugin] ) ) {
  60. $network_active = true;
  61. }
  62. }
  63. return in_array( $plugin, get_option( 'active_plugins' ) ) || $network_active;
  64. }
  65.  
  66. ## 4. Combining the methods
  67. // To get the best of both methods, class/function detection and active plugin detection, you can use:
  68.  
  69. # Example. Check if WooCommerce is active
  70. if ( main_is_plugin_active( 'woocommerce/woocommerce.php' ) || class_exists( 'WooCommerce' ) ) {
  71. // it's active so let's initialize our plugin.
  72. } else {
  73. // it's not active. Notify the user, perhaps displaying a notice.
  74. }
  75. # Example. Check if CMB2 is loaded
  76. if ( main_is_plugin_active( 'cmb2/init.php' ) || defined( 'CMB2_LOADED' ) ) {
  77. // it's active so let's initialize our file
  78. require_once get_template_directory() . '/functions/cmb2.php';
  79. } elseif ( is_admin() && current_user_can('manage_options') ) {
  80. // it's not active. Notify the user, perhaps displaying a notice.
  81. add_action( 'admin_notices', function(){
  82. echo '<div id="message" class="error notice is-dismissible"><p>CMB2 plugin <b>is not active!</b></p></div>';
  83. } );
  84. }
  85.  
  86. // With these methods we are now sure if your plugin is active or not and we can safely run ours.
Add Comment
Please, Sign In to add comment