Using WordPress ‘plugins_url’ PHP filter

The plugins_url WordPress PHP filter allows you to modify the URL to the plugins directory.

Usage

add_filter('plugins_url', 'your_custom_function', 10, 3);

function your_custom_function($url, $path, $plugin) {
    // your custom code here
    return $url;
}

Parameters

  • $url (string) – The complete URL to the plugins directory including scheme and path.
  • $path (string) – Path relative to the URL to the plugins directory. Blank string if no path is specified.
  • $plugin (string) – The plugin file path to be relative to. Blank string if no plugin is specified.

More information

See WordPress Developer Resources: plugins_url

Examples

Change plugins URL to a CDN

If you want to serve your plugins from a CDN, you can modify the URL.

function serve_plugins_from_cdn($url, $path, $plugin) {
    $cdn_url = 'https://cdn.example.com/wp-content/plugins';
    return $cdn_url . $path;
}
add_filter('plugins_url', 'serve_plugins_from_cdn', 10, 3);

Add a version query string to the plugins URL

To add a version query string to the plugins URL for cache busting purposes.

function add_version_to_plugins_url($url, $path, $plugin) {
    $version = '1.0.0';
    return $url . '?ver=' . $version;
}
add_filter('plugins_url', 'add_version_to_plugins_url', 10, 3);

Force SSL for plugins URL

Ensure that the plugins URL is always served over HTTPS.

function force_ssl_plugins_url($url, $path, $plugin) {
    return str_replace('http://', 'https://', $url);
}
add_filter('plugins_url', 'force_ssl_plugins_url', 10, 3);

Remove a specific plugin’s URL

If you want to remove the URL of a specific plugin.

function remove_specific_plugin_url($url, $path, $plugin) {
    if (strpos($plugin, 'plugin-to-remove') !== false) {
        return '';
    }
    return $url;
}
add_filter('plugins_url', 'remove_specific_plugin_url', 10, 3);

Append custom path to plugins URL

To append a custom path to the plugins URL.

function append_custom_path_to_plugins_url($url, $path, $plugin) {
    $custom_path = '/custom/path';
    return $url . $custom_path;
}
add_filter('plugins_url', 'append_custom_path_to_plugins_url', 10, 3);