Retrieve WordPress root directory path?

后端 未结 13 1371
闹比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:35

    Note: This answer is really old and things may have changed in WordPress land since.

    I am guessing that you need to detect the WordPress root from your plugin or theme. I use the following code in FireStats to detect the root WordPress directory where FireStats is installed a a WordPress plugin.

    function fs_get_wp_config_path()
    {
        $base = dirname(__FILE__);
        $path = false;
    
        if (@file_exists(dirname(dirname($base))."/wp-config.php"))
        {
            $path = dirname(dirname($base))."/wp-config.php";
        }
        else
        if (@file_exists(dirname(dirname(dirname($base)))."/wp-config.php"))
        {
            $path = dirname(dirname(dirname($base)))."/wp-config.php";
        }
        else
            $path = false;
    
        if ($path != false)
        {
            $path = str_replace("\\", "/", $path);
        }
        return $path;
    }
    
    0 讨论(0)
  • 2020-11-28 08:38

    You can use get_site_url() function to get the base url of the wordpress site.

    For more information, please visit http://codex.wordpress.org/Function_Reference/get_site_url

    0 讨论(0)
  • 2020-11-28 08:40

    Looking at the bottom of your wp-config.php file in the wordpress root directory will let you find something like this:

    if ( !defined('ABSPATH') )
        define('ABSPATH', dirname(__FILE__) . '/');
    

    For an example file have a look here:
    http://core.trac.wordpress.org/browser/trunk/wp-config-sample.php

    You can make use of this constant called ABSPATH in other places of your wordpress scripts and in most cases it should point to your wordpress root directory.

    0 讨论(0)
  • 2020-11-28 08:40

    This an old question, but I have a new answer. This single line will return the path inside a template: :)

    $wp_root_path = str_replace('/wp-content/themes', '', get_theme_root());
    
    0 讨论(0)
  • 2020-11-28 08:41

    Try this function for get root directory path:

    get_template_directory_uri();
    
    0 讨论(0)
  • 2020-11-28 08:42

    echo ABSPATH; // This shows the absolute path of WordPress

    ABSPATH is a constant defined in the wp-config.php file.

    0 讨论(0)
提交回复
热议问题