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.