WordPress Plugin-Path

When working on a Plugin I noticed that the path to its files is not always complete, which depends on the WordPress version.
With WordPress Version 2.8 the function plugins_url() extends and it can be incorporate in a much cleaner way, no maintenance of the folder required, where the Plugin is put. An example will illustrate it and possibly will help the one or other author at his work.

In the articles „Use JavaScript Libraries In And Of WordPress“, „Top Level Menu In WordPress 2.7“ and „WordPress - Return URL“ I often have the function plugins_url(), because it delivers very nice the path.

As of WordPress version 2.8 the function can take two parameters:

/**
 * @param string $path Optional. Path relative to the plugins url.
 * @param string $plugin Optional. The plugin file that you want to be relative to - i.e. pass in __FILE__
 * @return string Plugins url link with optional path appended.
*/
function plugins_url($path = '', $plugin = '')

So far only the $ path parameter could be passed, so that required a proper definition of the folder.

$style = plugins_url( 'my_plugin_folder/css/style-frontend.css' );

With the extension in version 2.8 of WordPress it can be done like:

$style = plugins_url( 'css/style-frontend.css', __FILE__ );

The use should make it simpler and clearer. But the question will come up, how do I do backwards compatibility of the plugin? You can either use the version query:

if ( version_compare( $wp_version, '2.8dev', '>' ) )

and then supplement or write a custom function for the replacement:

// function for WP < 2.8
function plugins_url($path = '', $plugin = '') {
  if ( function_exists('is_ssl') )
    $scheme = ( is_ssl() ? 'https' : 'http' );
  else
    $scheme = 'http';
  $url = WP_PLUGIN_URL;
  if ( 0 === strpos($url, 'http') ) {
    if ( function_exists('is_ssl') && is_ssl() )
      $url = str_replace( 'http://', "{$scheme}://", $url );
  }

  if ( !empty($plugin) && is_string($plugin) )
  {
    $folder = dirname(plugin_basename($plugin));
    if ('.' != $folder)
      $url .= '/' . ltrim($folder, '/');
  }

  if ( !empty($path) && is_string($path) && strpos($path, '..') === false )
    $url .= '/' . ltrim($path, '/');

  return apply_filters('plugins_url', $url, $path, $plugin);
}

This is only an alternative, the possibilities are endless - maybe one or the other has a beautiful idea and solution?

Comments are closed.

6 comments

  1. scribu

    This is the function I use in several of my plugins:


    function get_plugin_url() {
    // WP < 2.6
    if ( !function_exists('plugins_url') )
    return get_option('siteurl') . '/wp-content/plugins/' . plugin_basename(dirname(__FILE__));

    return plugins_url(plugin_basename(dirname(__FILE__)));
    }

    The advantage is that it works with all versions of WordPress from 2.3 to 2.8 and you don't have to hardcode the plugin directory.

  2. Frank

    @scribu: i think it is better you use your function with constants, with WordPress 2.5 it is possible to change the wp-content-folder in a new name or place, see a other post from me: Determine Path To Plugin and Content Directories
    Thanks for your comment and best wishes.

  3. scribu

    Yes, but that's exactly when the plugins_url() function got implemented, so that case is covered.

  4. scribu

    PS: It would be nice if commenters got a message when their comment is held for moderation.

    Keep up the good work.

  5. Michael

    scribu, we have also a life outside WPEngineer ;)

  6. Swashata

    Exactly what I was looking for! I needed that as I wanted to enqueue a script file to the wordpress admin :) Thanks a lot!