Url

Urls and paths in WordPress are hidden in all sorts of different functions and constants with different naming conventions. Cuisine’s Url class can help you out with most of these urls and paths.

The Url class is a Utility which basically means you can address all methods as statics, without using a wrapper of Facade. To use the Url class, include the class and namespace at the top of your php document:

use Cuisine\Utilities\Url


Theme Urls

Getting to your style.css file can be done like this:

Url::theme( 'style.css', false );


Let’s break that down:

Url::theme() is the function which looks at your active (child)theme.

The parameters passed down to the theme-function are, in order of appearence:

  • Subsequent path (optional) The first parameter is the folder or file you’de like to address, in this case style.css. It defaults to an empty string.

  • Trailing slash (optional) The second parameter (which is optional) adds a trailing slash if set to true. Default is false.

Theme Urls deal with the current active theme. If this is a child-theme, it will not address the parent.

Getting the url to a parent-theme:

Gettin the url to a parent-theme is pretty similar, it just uses a different function:

Url::parentTheme( 'style.css', false );

Plugin Urls

Getting the url to a certain file in your plugin ( like a script-file ) can be done like this:

Url::plugin( 'my-plugin/Assets/js/script.js', false );

Here’s a breakdown: >Url::plugin() gets the URl of your plugin directory and adds to it what you’ve passed in the first parameter.

> >The parameters passed down to the plugin-function are, in order of appearence: > >* Subsequent path (optional) > The first parameter is the folder or file you’de like to address, in this case script.js. It defaults to an empty string. > >* Trailing slash (optional) > The second parameter (which is optional) adds a trailing slash if set to true. Default is false. >


Paths

Paths are different than urls. They can point to the same files, but when loading or requiring files, often times paths are needed instead of urls.

Cuisine has one path function and gives you three shortcuts:

  • The active theme-directory
  • WordPress’ content-folder
  • The plugin directory

Here’s an example:

Url::path( 'theme', 'assets/js', true );

Here’s a break down: >Url::path() returns the absolute path for a few types for directories or files.

> >The parameters passed down to the plugin-function are, in orde of appearence: > >* Path type (optional) > Can be three things: theme, content or plugin. Defaults to plugin. > >* Subsequent path (optional) > The second parameter is the folder or file you’de like to address, in this case assets/js. It defaults to an empty string. > >* Trailing slash (optional) > The third parameter adds a trailing slash if set to true. Default is false. >


WordPress Javascript libraries:

WordPress comes with a lot of javascript-libraries, neatly minified and kept up-to-date by the core team. It’s a real shame not to use these libraries. But how do you go about getting their Urls? Cuisine gives you the Url::wp() function:

Url::wp( 'jquery/jquery.min.js' );

This function goes into the wp-includes/js folder. Here’s a breakdown: >Url::wp() returns the wp-includes/js folder together with the entire site url.

> >The parameter passed down to the plugin-function: > >* Subsequent path (optional) > The first and only parameter is the folder or file you’de like to address, in this case jquery/jquery.min.js. It defaults to an empty string. >