2

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?

0

3 Answers 3

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).

7
  • thanks I also thought of that and should have mentioned in OP question -- however, do you think it's reliable in 100% of the cases?
    – unfulvio
    Commented Feb 3, 2016 at 8:41
  • Yes, because that is the definition of where WordPress is installed. If it differs from the URL, it's a subdirectory install.
    – fischi
    Commented Feb 3, 2016 at 9:56
  • what if it's not a subdirectory install but the URLS are still different? For example sites which have different domains pointing to the same install
    – paul
    Commented Feb 4, 2016 at 10:17
  • @paul even then both options are the same, and the handling of multiple domains is done somewhere else. Do you have an example where this is not the case?
    – fischi
    Commented Feb 4, 2016 at 10:18
  • 1
    This is not a valid way to check if WordPress is in a subdirectory though. Because siteurl and home can be the same. For instance, domain.com/site1 is simply the site itself, that it's hosted on domain.com is irrelevant. Both siteurl and home can be set to https://domain.com/site1. I am just trying to say that siteurl and home are user input "fields" and they can be the same even if the site is located in a subdirectory. Of course this method will for sure work when the site root is the FQN and WP files are in a subdir. Commented May 12, 2020 at 18:41
0

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
0

Try this:

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

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.