2

Any idea why the following code

function my_plugin_load()
{
    get_plugins();
}
add_action( 'plugins_loaded', 'my_plugin_load' );

Throws this error?

Fatal error: Call to undefined function get_plugins()

Shouldn't get_plugins() be defined after the plugins_loaded hook is fired? If not, what would be the proper hook to call? (This hook should initiate the plugin's bootstrap/load process)

3
  • 1
    Have you looked at the examples on the get_plugins codex page?
    – Milo
    Jun 16, 2014 at 22:00
  • @Milo yes, that example would be one solution, but I was wondering if there was a solution that doesn't require me to explicitly include the file. Jun 16, 2014 at 22:06
  • 2
    There is no way to "not include the file". The codex explains that the function is, really, only designed for use in the admin side of WordPress - not for themes. If you want to use it in your theme go ahead and include the file as shown. Jun 17, 2014 at 1:13

1 Answer 1

6

Possibly you're trying to use that code in a non-admin page. The functon get_plugins is only available on admin side and if you ant to use it somewhere else you have to load the file "plugin.php" manually.

To solve this error add that check at the beginning of your function:

    if ( ! function_exists( 'get_plugins' ) ) {
        require_once ABSPATH . 'wp-admin/includes/plugin.php';
    }
1
  • This is what my debugging function needed to be able to work when using WordPress's REST API. Thanks. Oct 13, 2022 at 13:31

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.