0

This question is loosely related to Why some plugins are including wp-includes/plugin.php file?

What I'm trying to figure out is, when is it smart to include this file, and when isn't it?

I'm building a plugin that'd need the is_plugin_active() function in the frontend (where it doesn't exist, because it's only loaded in wp-admin.

My gut says it's a waste (of memory or performance, maybe?) to include the entire wp-admin/includes/plugin.php just for the purpose of loading one function. So, IMO, a better idea would be to copy the is_plugin_active() function to my own plugin and run it from there.

Am I right about this? Or would you say: No, just require_once() the plugin.php file -- even if you only need one function from there? And if so, why?

8
  • What are you trying to do? Detect if another plugin you depend on is active?
    – Rup
    May 21, 2023 at 21:05
  • I think including the file is right, unless it starts a cascade of including all the other admin code. Other plugins might be doing the same thing. If it’s only a few extra functions that won’t be significant extra resources.
    – Rup
    May 21, 2023 at 21:07
  • @Rup I'm building a plugin which integrates a popular accounting software with a WordPress ecommerce plugin. During checkout, I'm also checking if certain 3rd party EU VAT plugins are active, so they can provide data about the used VAT rates or reverse charging. May 22, 2023 at 5:51
  • 1
    Another option is you could check for the functions or classes you'd be calling, e.g. with class_exists or function_exists.
    – Rup
    May 22, 2023 at 9:03
  • 1
    Oh, right, sorry I meant if (class_exists('WC_EU_VAT_Compliance_Rates')) etc. rather than for is_plugin_active to see if its plugin is loaded.
    – Rup
    May 22, 2023 at 13:36

1 Answer 1

2

So, the answer isn't as straightforward as I initially hoped.

A quick test using memory_get_usage() on a local environment with only Twenty Sixteen enabled as the theme gives the following results:

Memory usage before requiring plugin.php: 8496336
Memory usage after requiring plugin.php: 8583912

So, loading plugin.php takes up 87.576 bytes, or roughly 85KB. If you only need one function from it, that's a lot.

Now, when I copy the function to my own code and measure memory usage again:

Memory usage before copied function: 8436128
Memory usage after copied function: 8436240 

Now the difference is around 100 bytes. That's a significant difference!

From this we can conclude that in terms of effiency it would be smarter to copy the function from plugin.php into your own code. You might even argue that it's a shame that so many devs have chosen the easy route and simply imported wp-admin functionalities to the frontend.

But, this is exactly where the answer becomes vague.

There are A LOT of (very popular) plugins out there that're already loading wp-admin/includes/plugin.php in the frontend. In my local environment ~20%. So, you might say that at this point it's become a standard.

A few examples:

  • AffiliateWP
  • Avada (theme)
  • Easy Digital Downloads
  • Kirki-based themes
  • WP Optimize

And, funny detail, core WordPress does it, too, in the WP_REST_Controller classes. You might even wonder why they haven't bothered moving it to a place where it's more widely available.

If you're building a publically available plugin (like I am), it's very likely your users are already using another plugin which is loading plugin.php. So, memory usage and/or performance won't take a hit if you simply do:

if (!function_exists('is_plugin_active')) {
    require_once(ABSPATH . '/wp-admin/includes/plugin.php';
}

I think it is good practice to check specifically for the functions you require, but other than that, at this point I'm saying: go for it.

2
  • 1
    It is accepted practice to simply include the file that has the function (though I would agree it's debatable as best practice). Another option is if is_plugin_active() does not exist, to define your own function. That would allow you to have reduced memory usage when possible. If the plugin is utilizing some sort of testing, writing tests to compare core is_plugin_active() with plugin's function would ensure detecting incompatibility.
    – Caleb
    May 31, 2023 at 16:32
  • I like that idea. I'll see if I can fit that into my code in a logical place. Thanks! May 31, 2023 at 20:21

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.