Retrieve WordPress root directory path?

后端 未结 13 1406
闹比i
闹比i 2020-11-28 07:48

How can I retrieve the path to the root directory in WordPress CMS?

13条回答
  •  南方客
    南方客 (楼主)
    2020-11-28 08:26

    I like @Omry Yadan's solution but I think it can be improved upon to use a loop in case you want to continue traversing up the directory tree until you find where wp-config.php actually lives. Of course, if you don't find it and end up in the server's root then all is lost and we return a sane value (false).

    function wp_get_web_root() {
    
      $base = dirname(__FILE__);
      $path = false;
    
      while(!$path && '/' != $base) {
        if(@file_exists(dirname($base)).'/wp-config.php') {
          $path = dirname($base);
        } else {
          $base = dirname($base);
        }
      }
    
      return $path;
    }
    

提交回复
热议问题