WPLib::get_root_url PHP Method

get_root_url() static public method

Get the root URL for a given Lib/Site/App/Module/Theme.
static public get_root_url ( string $filepath, boolean | string $class_name = false ) : string
$filepath string Name of path to append to root URL.
$class_name boolean | string Name of class to return the root dir.
return string
    static function get_root_url($filepath, $class_name = false)
    {
        if (!$class_name) {
            $class_name = get_called_class();
        }
        if (!isset(self::$_root_urls[$class_name])) {
            $root_dir = static::get_root_dir('', $class_name);
            if (preg_match('#^' . preg_quote(get_stylesheet_directory()) . '(.*)#', $root_dir, $match)) {
                /**
                 * If in the theme directory
                 */
                $root_url = get_stylesheet_directory_uri() . (isset($match[1]) ? $match[1] : '');
            } else {
                /**
                 * Or if in the plugins directories
                 */
                $root_url = plugins_url('', $root_dir . '/_.php');
            }
            self::$_root_urls[$class_name] = rtrim($root_url, '/');
        }
        $filepath = '/' . ltrim($filepath, '/');
        return self::get_real_url(self::$_root_urls[$class_name] . $filepath);
    }

Usage Example

Ejemplo n.º 1
0
 /**
  * @param string $filepath
  * @return string
  */
 function get_root_url($filepath)
 {
     return WPLib::get_root_url($filepath, get_class($this));
 }