TitanFramework::getURL PHP Method

getURL() public static method

Acts the same way as plugins_url( 'script', __FILE__ ) but returns then correct url when called from inside a theme.
Since: 1.1.2
public static getURL ( string $script, string $file ) : string
$script string the script to get the url to, relative to $file.
$file string the current file, should be __FILE__.
return string the url to $script
    public static function getURL($script, $file)
    {
        $parentTheme = trailingslashit(get_template_directory());
        $childTheme = trailingslashit(get_stylesheet_directory());
        $plugin = trailingslashit(dirname($file));
        // Windows sometimes mixes up forward and back slashes, ensure forward slash for correct URL output.
        $parentTheme = str_replace('\\', '/', $parentTheme);
        $childTheme = str_replace('\\', '/', $childTheme);
        $file = str_replace('\\', '/', $file);
        $url = '';
        // Framework is in a parent theme.
        if (stripos($file, $parentTheme) !== false) {
            $dir = trailingslashit(dirname(str_replace($parentTheme, '', $file)));
            if ('./' == $dir) {
                $dir = '';
            }
            $url = trailingslashit(get_template_directory_uri()) . $dir . $script;
        } else {
            if (stripos($file, $childTheme) !== false) {
                // Framework is in a child theme.
                $dir = trailingslashit(dirname(str_replace($childTheme, '', $file)));
                if ('./' == $dir) {
                    $dir = '';
                }
                $url = trailingslashit(get_stylesheet_directory_uri()) . $dir . $script;
            } else {
                // Framework is a or in a plugin.
                $url = plugins_url($script, $file);
            }
        }
        // Replace /foo/../ with '/'.
        $url = preg_replace('/\\/(?!\\.\\.)[^\\/]+\\/\\.\\.\\//', '/', $url);
        return $url;
    }

Usage Example

コード例 #1
0
 /**
  * Enqueues the jQuery UI scripts
  *
  * @return	void
  * @since	1.4
  */
 public function enqueueDatepicker()
 {
     wp_enqueue_script('jquery-ui-core');
     wp_enqueue_script('jquery-ui-slider');
     wp_enqueue_script('jquery-ui-datepicker');
     wp_enqueue_script('tf-jquery-ui-timepicker-addon', TitanFramework::getURL('js/min/jquery-ui-timepicker-addon-min.js', __FILE__), array('jquery-ui-datepicker', 'jquery-ui-slider'));
 }
All Usage Examples Of TitanFramework::getURL