admin管理员组

文章数量:1124679

I want to check if a WordPress installation is working from a subdirectory.

Common methods to achieve this are listed here:

these usually involve changing an index file and requiring WordPress main file from there, perhaps changing rules in htaccess if using Apache - there's no setting up constants from what I can see

so, how would you detect reliably if WordPress is not running from a directory root?

I want to check if a WordPress installation is working from a subdirectory.

Common methods to achieve this are listed here: https://codex.wordpress.org/Giving_WordPress_Its_Own_Directory

these usually involve changing an index file and requiring WordPress main file from there, perhaps changing rules in htaccess if using Apache - there's no setting up constants from what I can see

so, how would you detect reliably if WordPress is not running from a directory root?

Share Improve this question asked Feb 2, 2016 at 10:02 unfulviounfulvio 1,8247 gold badges32 silver badges63 bronze badges 0
Add a comment  | 

3 Answers 3

Reset to default 2

You need to check if the siteurl differs from the home URL:

if ( get_option( 'siteurl' ) !== get_option( 'home' ) ) { // whatever

This also works if home is a subdirectory of the root, with WordPress installed in yet another subdirectory. For example: domain.com/blog (URL) and domain.com/blog/wordpress (siteurl).

I just came across for the same question and found another way to figure it out. You can also use the defined Constants from the wp-config.php file. SUBDOMAIN_INSTALL would be the key.

Example wp-config excerpt for Multisite settings would be

define('WP_ALLOW_MULTISITE', true );
define('MULTISITE', true);
define('SUBDOMAIN_INSTALL', false);
define('DOMAIN_CURRENT_SITE', 'intern.cc.dev');
define('PATH_CURRENT_SITE', '/');

In your code

// only run condition, when we are in MS
if( is_multisite() ):

   // check for subfolder installation flag
   if( SUBDOMAIN_INSTALL === false):

      // optional, check if you are on the home WP or a sub WP
      if( get_current_blog_id() > 1):

         // do whatever you need to to here


      endif; // end blog id check
   endif; // end subfolder installation 
endif; // end multisite check

Try this:

if ( '/' !== site_url( '', 'relative') ) {

本文标签: directoryHow would you detect if WordPress is installed in a subdirectory (not root)