skip to Main Content

How to detect if a folder is having a WordPress installation or not ?

WP-CLI does that and gives the error This does not seem to be a WP installation, and detects correctly if the directory has a WP install even if it is in any of the subfolders (wp-includes/images or wp-includes/js ) .

I went through the code and it searches for index.php and compares content with the original index.php . One more thing it does is to check for the presence of wp-includes/version.php . Got the idea but how it works on subfolders like those mentioned above is still not clear . Do anybody have any idea on how to do this ? Thanks in advance .

2

Answers


  1. Chosen as BEST ANSWER

    So i have scribbled a script that detects a WP install . It works as expected inside a wp install folder , but if it is not inside a WordPress install it executes an infinite loop . Can somebody guide on how to stop at top level directory as mentioned by @O.Jones ? Here is my code .

    <?php
    
    function get_wp_index($dir=null) {
    
    
    if(is_null($dir)){
        $dir = getcwd();
    }
    
    echo "Currently Looking n";
    echo $dir;
    
    $name = $dir.DIRECTORY_SEPARATOR."index.php";
    
    if ( file_exists( $name ) ) {
    
    
        $index_code = (file_get_contents($name));
    
        if ( preg_match( '|^s*requires*(?s*(.+?)/wp-blog-header.php(['"])|m', $index_code, $matches ) ) {
    
            echo "Is a WP Install";
    
            return;
    
        } else {
            
            echo "Has index File but not one with wp-blog-header";
            echo "nn";
            //Go one directory up
            $up_path = realpath($dir. DIRECTORY_SEPARATOR . '..');
            get_wp_index($up_path);
    
        }
    } else {
    
        echo 'No Index File Found';
        echo "nn";
            //Go one directory up
            $up_path = realpath($dir. DIRECTORY_SEPARATOR . '..');
        echo $up_path;
        get_wp_index($up_path);
    
      }
    }
    
    get_wp_index();
    ?>
    

  2. Look for the wp-config.php file. If you find it, require it, then try to use its constants DB_HOST, DB_USER, DB_PASSWORD and DB_NAME to connect to the WordPress database associated with the WordPress instance. If that works, you very likely have a working WordPress instance.

    If your current working directory doesn’t have wp-config.php look at parent directories recursively until you (a) find it or (b) come to the top level directory.

    wp-cli does more elaborate things. But this should work for you.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search