Читать книгу Professional WordPress Plugin Development - Brad Williams - Страница 48

URL Paths

Оглавление

Referencing URL paths can be tougher than local paths because there's usually no good way to determine this via standard PHP functions or constants alone. You'll need to rely on WordPress to get the correct path.

The primary use case for getting a URL path will be determining the path to an asset (e.g., JavaScript, CSS, or image files) within your plugin. WordPress provides the plugin_dir_url() function to handle this use case.

<?php $url = plugin_dir_url( $file ); ?>

Parameters:

 $file (string, required): The filename in the current directory path

WordPress will automatically convert any filename passed in to an appropriate URL equivalent. See the following example of passing in the filename from within the primary plugin file:

<?php echo plugin_dir_url( __FILE__ ); ?>

That code will output something like the following URL with a trailing slash:

https://example.com/wp-content/plugins/pdev/

If you wanted to determine the path of a JavaScript file located at /public/js/example.js in your plugin, you'd use the following code:

<?php $url = plugin_dir_url( __FILE__ ) . 'public/js/example.js'; ?>

You can use this to correctly get the URL path to any location in your plugin. Like its plugin_dir_path() counterpart, you can pass in any filename in your plugin to determine the appropriate URL for any location.

WordPress also provides a plugins_url() function. plugin_dir_url() is a wrapper for this function. For most purposes, these two functions can almost be used interchangeably, but there are some important differences.

<?php $url = plugins_url( $path = '', $plugin = '' ); ?>

Parameters:

 $path (string, optional): A path to append to the end of the URL

 $plugin (string, optional): The full path to a file within the plugin

If no parameters are provided, the function will return the URL to the plugin's directory for the WordPress installation. It also does not add a trailing slash to the end of the URL. For most cases, it's usually best to stick with plugin_dir_url(). However, this function is available if needed.

Other than determining the URL path to files within your plugin, you may need to determine the URL for a particular page or directory within the WordPress installation. WordPress has a number of useful functions that return the necessary information.

 site_url(): URL path to where WordPress is installed

 home_url(): URL path to the site's homepage

 admin_url(): URL path to the WordPress admin

 rest_url(): URL path the REST API endpoint

 includes_url(): URL path to the WordPress includes directory

 content_url(): URL path to the WordPress content directory

All of these URL functions accept an optional first parameter of $path, which is appended to the end of the URL if provided. The following example shows how to retrieve the URL path to the General Settings page in the admin:

<?php $url = admin_url( 'options-general.php' ); ?>

With the exception of the content_url() function, each of these functions also accepts an optional second parameter of $scheme, which allows you to manually set the protocol, such as 'http' or 'https'. In almost all cases, you should not set this parameter and should instead allow WordPress to automatically determine the appropriate URL scheme.

First‐time WordPress developers will also sometimes confuse site_url() and home_url(). WordPress can be installed in a subdirectory on the server while allowing the actual site to be located elsewhere. Unfortunately, the function names are part of a legacy code base and have stuck around. The trick is to remember that the “home” in home_url() refers to the URL of the site, and the “site” in site_url() refers to the URL of the WordPress installation.

If WordPress is installed in a subdirectory, site_url() might point to a URL like https://example.com/wordpress, while home_url() points to https://example.com.

Professional WordPress Plugin Development

Подняться наверх