پیشرفته: آیا پلاگین وردپرسی فعال هست؟

توسط: س سروش | ۲۲ اردیبهشت ۱۳۹۳ | کدهای وردپرس | 1,051 بازدید

When developing plugins, there comes a time when you build features that enhance or compliment other popular plugins. For example, FooBox does this for a number of popular plugins including WooCommerceJetPackNextGen and Yoast’s WordPress SEO. I check if those plugins are installed and activated and then I show extra settings on the FooBox settings page. Very cool! But sometimes it is not that straight forward to know if a plugin is active. Here are some methods you can use:

Use the WordPress Function (is_plugin_active)

You can use the built in WordPress function is_plugin_active to do the check.

Here is an example where I check if Akismet is activated:

The catch : you need to know the directory and file name of the plugin. The directory could differ if the plugin was manually installed. Although very unlikely, the file name could change in future plugin updates.

Check If a Class Is Defined (class_exists)

You can check if a specific class exists using the class_exists function. This means you have to go through the plugin code to find a class that is defined, but it should not be too difficult.

Here is an example where I check if the WooCommerce plugin is activated:

The catch : the plugin you are checking for needs to define a unique class – some plugins do not. The class name could also change in future plugin updates.

Check If a Named Constant Is Defined

You can check if a specific named constant is defined and then perhaps even do further checks if it is.

In the below example, I am checking if NextGen V2 is activated. I first check if the NEXTGEN_GALLERY_PLUGIN_VERSION constant is defined, and then if it is, I check the value to make sure it is at least 2.0.0.

In most cases, you would only need to check for the existance of the constant, but in this case I had to do some more work for my particular scenario.

The catch : the plugin you are checking for needs to define a constant – some plugins do not. The name of the constant could change in a future plugin updates.

Check If a Function Name Is Defined (function_exists)

If a plugin does not define any classes or constants, you could check for one of it’s unique, prefixed function names using the function_exists function.

Here I am checking if the Hello Dolly plugin is activated:

The catch : the plugin needs to use a unique, prefixed function name. The function name could change in future updates.

What I Recommend

I have never used the function_exists check, but realized that it is an option when I wrote this post. I have recently had to check if a specific named constant was defined, as in my NextGen example above. I personally use (and prefer) the class_exists method in most scenarios. I think (or hope) that the class names in a plugin should remain constant and never change. I feel semi-uncomfortable using the is_plugin_active function, but that is mostly due to paranoia (probably unfounded, in case a plugin is manually installed by FTP to a different directory). What method do you use?

دیدگاهتان را بنویسید

نشانی ایمیل شما منتشر نخواهد شد. بخش‌های موردنیاز علامت‌گذاری شده‌اند *

This site uses Akismet to reduce spam. Learn how your comment data is processed.