'Function get_plugins() cannot be execute

I'm trying to code simple php script which will return all plugins of Wordpress.

My code:

<?php 
ini_set('display_errors', 'On');

include('../../../wp-admin/includes/plugin.php');

$all_plugins = get_plugins();

error_log( print_r( $all_plugins, true ) );

And my output is:

 Uncaught Error: Call to undefined function wp_cache_get() in /home/user/domains/mydomain.com/public_html/wp-admin/includes/plugin.php:273 Stack trace: #0 /home/user/domains/mydomain.com/public_html/wp-content/plugins/myplugin/versions.php(6): get_plugins() #1 {main} thrown in /home/user/domains/mydomain.com/public_html/wp-admin/includes/plugin.php on line 273

I used official example from: https://codex.wordpress.org/Function_Reference/get_plugins

I did:

  • change of PHP version from 7.2 to 5.6
  • set right permission of files
  • install WordPres once again

So my wordpress is totally fresh and I still have the same error. Somebody knows how to solve it?



Solution 1:[1]

To load all the WordPress functions you need to include the file wp-load.php:

require_once("../../../../wp-load.php");

and not the plugins one.

Solution 2:[2]

Use at the top of your plugin.

include_once( ABSPATH . 'wp-admin/includes/plugin.php' );

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Enrique Chavez
Solution 2 Saiful Islam