Using includes_url to get WordPress includes folder URL

The includes_url function in WordPress is used to retrieve the URL of the includes directory. This directory typically contains important files that are used by WordPress to perform various tasks and functionalities. The returned URL can be used in situations where there is a need to reference or link to files located within the includes directory.

For instance, this function can be used when enqueuing scripts or styles that are located in the includes directory. It can also be used when there is a need to include a file from the includes directory in the theme or plugin. This function is designed to ensure that the correct URL is always retrieved, regardless of the site’s configuration or the location of the WordPress installation.

The includes_url function is part of the WordPress Filesystem API, which provides a set of functions and tools designed to read, write, and manipulate files and directories in a safe and consistent manner.

Parameters Accepted by the includes_url Function

The includes_url function in WordPress is designed to accept two specific parameters. These are:

  • $path: This is a string parameter and is optional. By default, its value is an empty string. This parameter is used to specify a path that is relative to the includes URL.
  • $scheme: This is also a string parameter and is optional. Its default value is null. This parameter is used to provide a context to the includes URL. It accepts three values: ‘http’, ‘https’, or ‘relative’.

Return Value of the includes_url Function

The includes_url function returns a string. This string is essentially the Includes URL link and it may have an optional path appended to it. This function is particularly useful when you need to generate a URL for a specific file or directory within the WordPress includes directory.

Examples

How to include a JavaScript file using includes_url function

function enqueue_my_script() {
 wp_enqueue_script( 'my-script', includes_url( 'js/my-script.js' ), array( 'jquery' ), '1.0', true );
}
add_action( 'wp_enqueue_scripts', 'enqueue_my_script' );

In the above example, the includes_url function is used to include a JavaScript file named ‘my-script.js’ located in the ‘js’ directory of the WordPress includes folder. The script is enqueued using the wp_enqueue_script function and the action is hooked to the wp_enqueue_scripts action hook.

How to include a CSS file using includes_url function

function enqueue_my_style() {
 wp_enqueue_style( 'my-style', includes_url( 'css/my-style.css' ), array(), '1.0' );
}
add_action( 'wp_enqueue_scripts', 'enqueue_my_style' );

In this example, the includes_url function is used to include a CSS file named ‘my-style.css’ located in the ‘css’ directory of the WordPress includes folder. The style is enqueued using the wp_enqueue_style function and the action is hooked to the wp_enqueue_scripts action hook.

Conclusion

The WordPress includes_url function is designed to retrieve the URL of the includes directory, where WordPress core files like JavaScript, CSS, and other utility files are stored. It’s particularly useful for developers who need to link to these core resources directly in their themes or plugins, ensuring a reliable reference to essential WordPress functionality.

Related WordPress Functions