WordPress – Return URL

I already talked in my article Determine Path To Plugin and Content Directories about the possibilities how to keep functions in themes or Plugins free from error if you rename a folder, for example wp-content.

Since version 2.6 you can also solve this problem pretty easy with another function.
More details and information are available in /wp-includes/link-templates.php.

Return the site url

Returns the ‘site_url’ option with the appropriate protocol, ‘https’ if is_ssl() and ‘http’ otherwise.
If $scheme is ‘http’ or ‘https’, is_ssl() is overridden.

$path – Optional path relative to the site url
$scheme Optional scheme to give the site url context. Currently ‘http’,’https’, ‘login’, ‘login_post’, or ‘admin’

site_url($path = '', $scheme = null)

Return the admin url

Returns the url to the admin area.

$path – Optional path relative to the admin url

admin_url($path = '')

Return the includes url

Returns the url to the includes directory.

$path – Optional path relative to the includes url

includes_url($path = '')

Return the content url

Returns the url to the content directory

$path – Optional path relative to the content url

content_url($path = '')

Return the Plugins url

Returns the url to the Plugins directory.

$path – Optional path relative to the Plugins url

plugins_url($path = '')

At version 2.8 of WordPress you can use two parameter. Use $plugin with __FILE__ for scan your folder.

plugins_url($path = '', $plugin = '')

Example for Pluginpath

The follw code load a javascript from the Plugin in folder examble_plugin_folder.

wp_enqueue_script( 'farbtastic', plugins_url('/examble_plugin_folder/includes/farbtastic/farbtastic.js'), array('jquery'), '1.2' );

I hope you enjoy this alternative. please comment for a better idea or a other ways to do it.


Posted

in

by

Comments

4 responses to “WordPress – Return URL”

  1. […] WP_CONTENT_DIR and WP_PLUGIN_DIR constants for answers, but a better solution is likely to use the X_url() functions. The most useful of those is likely to be plugins_url(). Even better, you can give these functions […]

  2. Sebastien Avatar
    Sebastien

    Thank you for this post-it, this is a complete work-sheet for relatives URLs.

    May be an example showing how to use it inside the DOM code directly would satisfy the most newbies of us.

    Cheers,

  3. Mike Schinkel Avatar

    Nice post!

    BTW, typo in heading: Examble for Pluginpath

  4. Michael Avatar

    Thanks Mike, both fixed.