
WordPress Plugin Development Cookbook
By :

On occasion, plugins need to refer to external files (for example, images, JavaScript, or jQuery script files) that are stored in the plugin directory. Since users are free to rename a plugin's folder or even install plugin files straight into the WordPress plugins
directory, paths to any external files must be built dynamically based on the actual plugin location. Thankfully, a number of utility functions are present to simplify this task.
Follow these steps to create a simple plugin that will add a favicon meta tag to a website's header, pointing to an image file located in the plugin's directory:
plugins
directory of your development installation.ch2-favicon
.ch2-favicon
directory with its default name (favicon.ico
).ch2-favicon
directory and create a new text file called ch2-favicon.php
.Chapter 2 - Favicon
.add_action( 'wp_head', 'ch2fi_page_header_output' );
ch2fi_page_header_output
function:function ch2fi_page_header_output() { $site_icon_url = get_site_icon_url(); if ( !empty( $site_icon_url ) ) { wp_site_icon(); } else { $icon = plugins_url( 'favicon.ico', __FILE__ ); ?> <link rel="shortcut icon" href="<?php echo esc_url( $icon ); ?>" /> <?php } }
Figure 2.3 – The website favicon appearing in Microsoft Edge, Google Chrome, and Mozilla Firefox
favicon.ico
file.The plugins_url
utility function, used in conjunction with the __FILE__
PHP constant and the name of our favicon file, allows us to quickly get the URL of this file located in our plugin directory. Once we have it, we can print out the appropriate HTML command to notify browsers of the location of this file.
The plugins_url
function can be called with or without parameters. In the first case, it builds a URL by appending the path or filename found in the first parameter to the location of the file specified in the second argument. In the second situation, it simply returns the location of the plugin directory:
plugins_url( $path, $plugin );
Before we display our plugin's favicon file, we also call the get_site_icon_url
function to see whether the user has already assigned a site icon using the WordPress customizer. If that is the case, we give priority to that icon and display it using the wp_site_icon()
function.
Note
The Twenty Twenty-Two theme found in WordPress 5.9 is built using a new theme creation technique and does not have a customizer section. This is why we change our site theme to demonstrate how our code takes into consideration site icons assigned in the customizer. The majority of themes available today have customizer sections, but this may change as WordPress evolves. The Twenty Twenty-Two theme may also add a customizer section in future releases.
The plugins_url
function is one of the many functions that can be used in plugins to help find the location of files in a WordPress installation. Other useful functions include the following:
get_theme_root()
: Returns the address of the theme installation directoryget_template_directory_uri()
: Retrieves the URL to the current theme's filesadmin_url()
: Provides the address of the WordPress administrative pagescontent_url()
: Indicates where the wp-content
directory can be foundsite_url()
and home_url()
: Returns the site addressincludes_url()
: Provides the location of WordPress include fileswp_upload_dir()
: Indicates the directory where user-uploaded files are stored