Professional WordPress Plugin Development. Brad Williams

Чтение книги онлайн.

Читать онлайн книгу Professional WordPress Plugin Development - Brad Williams страница 16

Professional WordPress Plugin Development - Brad Williams

Скачать книгу

first is the local path to files on the server. The second is URL paths, which may be necessary for loading JavaScript files, CSS stylesheets, or other assets. You may also need to link to a specific URL within the WordPress installation itself.

      Because WordPress provides the ability for users to move their wp‐content folder (where plugins are located) to any location on the server, it's important to use the standard WordPress functions for determining the correct path.

      Local Paths

      Local paths reference locations on the server. PHP provides easy methods for determining paths for almost every case needed. However, to be on the safe side, it's best to use WordPress functions where possible. The plugin_dir_path() function provides an easy way to get the filesystem directory path to your plugin.

      <?php $path = plugin_dir_path( $file ); ?>

      Parameters:

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

      The following example will print the path of the current directory of the file passed in with a trailing slash using PHP's __FILE__ constant:

      That code should output a path similar to the following:

      /public_html/wp-content/plugins/pdev/

      Any file can be passed into the function. The resulting path will point to wherever the file lives in your plugin. If passing in __FILE__ from the primary plugin file, it'll be the path to the root of your plugin. If passing it in from a subfolder in your plugin, it will return the path to the subfolder in your plugin.

      Assuming you needed to load a file named /src/functions.php from your plugin that houses some custom functions, use the following code:

      <?php include plugin_dir_path( __FILE__ ) . '/src/functions.php'; ?>

      It is common practice for plugin authors to store this path as a variable or constant in the plugin's primary file for quick access to the plugin's root folder path, as shown in the following example code:

      <?php define( 'PDEV_DIR', plugin_dir_path( __FILE__ ) ); ?>

      This allows you to reference PDEV_DIR any time you need it from anywhere in the plugin without having to think about file paths.

      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.

      <?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

Скачать книгу