I want to get just the subdirectory name from the network site, and I've gone through the codex and cannot find a function that already does this.

I have come up with a few options to get to the point I need, but I wanted to know if there was either:

  1. a function I havent seen yet
  2. which of these options works the best and efficiently

Option 1:

$url = get_bloginfo( 'url' );                     // output http://example.com/site1
$str = preg_replace( '#^https?://#', '', $url );  // output example.com/site1
$dom = parse_url( $url );
$dom = str_replace( 'www.', '', $dom['host'] );   // output example.com

$out = str_replace( $dom . '/', '', $str );       // just the subdirectory

Option 2:

$url = get_blog_details()->path;                 // output /site1/
$dom = str_replace( '/', '', $url );             // output site1

Obviously option 2 is less lines, but I'm not sure if it is the most efficient or cleanest.


The only thing more efficient code-wise than your Option 2 would be this one-liner:

$dom = str_replace( '/', '', get_blog_details()->path );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.