I want to run a PHP script that iterates over WordPress sites found in a certain folder and read the plugins info per each site using get_plugin function

In the code below, I try to require API functions inside the WP folder and use it to retrieve data. However, the problem is the multiple "require".

Sample code:

function get_sites_plugins() {
    foreach($this->all_sites as $site_folder) {
        $plugins = get_site_plugins($site_folder);
        echo "Info for site $site_folder";

function get_site_plugins($site_folder) {

    // define site path
    $site_path = $CONST_SITES_DIR . $site_folder;

    // defines ABSPATH

    // define get_plugins()
    require ABSPATH . 'wp-admin/includes/plugin.php'; 

    // PROBLEM: only first require works, need to "unrequire" but that's not possible.

    // get plugins
    $all_plugins = get_plugins();

    return $all_plugins;


How do I achieve this?

Note that the PHP script running is on the same host server.


2 Answers 2


Write a bash script (or any other shell script) to loop ocer the site and in php just write the detection for a single site and output the value to standard output or a file.

Or use http://wp-cli.org/ (which essentially does for you the php code from the paragraph above).


Have you tried require-once function?

  • that can still cause problems if there are conflicting usages of hooks. Commented Aug 4, 2015 at 4:15
  • yes, how do you think it would help in this case? it will not override the already defined constants / functions. Commented Aug 4, 2015 at 15:12
  • Are you dealing with a multi-sites WordPress or number of separate websites? In the first instance all plugins are in one folder, in the latter instance the require "function" should not be conflicting as it loads a new URL every time for each website.
    – Kalimah
    Commented Aug 4, 2015 at 23:44

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.