url

Versions
4.6 – 5
url($path = NULL, $query = NULL, $fragment = NULL, $absolute = FALSE)
6
url($path = NULL, $options = array())
7
url($path = NULL, array $options = array())

Generate a URL from a Drupal menu path. Will also pass-through existing URLs.

When creating links in modules, consider whether l() could be a better alternative than url().

Parameters

$path The Drupal path being linked to, such as "admin/node", or an existing URL like "http://drupal.org/".

$query A query string to append to the link or URL.

$fragment A fragment identifier (named anchor) to append to the link. If an existing URL with a fragment identifier is used, it will be replaced. Note, do not include the '#'.

$absolute Whether to force the output to be an absolute link (beginning with http:). Useful for links that will be displayed outside the site, such as in an RSS feed.

Return value

a string containing a URL to the given path.

Related topics

▾ 97 functions call url()

aggregator_help in modules/aggregator.module
Implementation of hook_help().
aggregator_page_rss in modules/aggregator.module
Menu callback; generate an RSS 0.92 feed of aggregator items or categories.
aggregator_page_sources in modules/aggregator.module
Menu callback; displays all the feeds used by the aggregator.
archive_help in modules/archive.module
Implementation of hook_help().
block_admin_display in modules/block.module
Generate main block administration form.
block_box_form in modules/block.module
block_help in modules/block.module
Implementation of hook_help().
blogapi_blogger_get_users_blogs in modules/blogapi.module
blogapi_blogger_get_user_info in modules/blogapi.module
Blogging API callback. Returns profile information about a user.
blogapi_help in modules/blogapi.module
Implementation of hook_help().
blogapi_menu in modules/blogapi.module
blogapi_rsd in modules/blogapi.module
blog_feed_last in modules/blog.module
Displays an RSS feed containing recent blog entries of all users.
blog_feed_user in modules/blog.module
Displays an RSS feed containing recent blog entries of a given user.
blog_help in modules/blog.module
Implementation of hook_help().
blog_page_last in modules/blog.module
Displays a Drupal page containing recent blog entries of all users.
blog_page_user in modules/blog.module
Displays a Drupal page containing recent blog entries of a given user.
book_help in modules/book.module
Implementation of hook_help().
comment_configure in modules/comment.module
Menu callback; presents the comment settings page.
comment_form in modules/comment.module
comment_help in modules/comment.module
Implementation of hook_help().
comment_nodeapi in modules/comment.module
Implementation of hook_nodeapi().
contact_admin_settings in modules/contact.module
Settings tab. Using a form rather than hook_settings().
contact_help in modules/contact.module
Implementation of hook_help().
contact_mail_page_submit in modules/contact.module
Process the site-wide contact page form submission.
contact_mail_user in modules/contact.module
Personal contact page.
contact_mail_user_submit in modules/contact.module
Process the personal contact page form submission.
contact_user in modules/contact.module
Implementation of hook_user().
drupal_goto in includes/common.inc
Send the user to a different Drupal page.
drupal_help in modules/drupal.module
Implementation of hook_help().
drupal_settings in modules/drupal.module
Implementation of hook_settings().
file_create_url in includes/file.inc
Create the download path to a file.
filter_help in modules/filter.module
Implementation of hook_help().
forum_help in modules/forum.module
Implementation of hook_help().
forum_overview in modules/forum.module
Returns an overview list of existing forums and containers
help_main in modules/help.module
Menu callback; prints a page listing a glossary of Drupal terminology.
hook_ping in developer/hooks/core.php
Ping another server.
hook_search in developer/hooks/core.php
Define a custom search routine.
l in includes/common.inc
Format an internal Drupal link.
locale_help in modules/locale.module
Implementation of hook_help().
menu_configure in modules/menu.module
Menu callback; presents menu configuration options.
menu_form_alter in modules/menu.module
Implementation of hook_form_alter(). Add menu item fields to the node form.
menu_get_active_help in includes/menu.inc
Returns the help associated with the active menu item.
menu_help in modules/menu.module
Implementation of hook_help().
node_admin_search in modules/node.module
node_block in modules/node.module
Implementation of hook_block().
node_feed in modules/node.module
A generic function for generating RSS feeds from a set of nodes.
node_help in modules/node.module
Implementation of hook_help().
node_page_default in modules/node.module
Generate a listing of promoted nodes.
node_search in modules/node.module
Implementation of hook_search().
page_help in modules/page.module
Implementation of hook_help().
path_help in modules/path.module
Implementation of hook_help().
ping_help in modules/ping.module
Implementation of hook_help().
poll_help in modules/poll.module
Implementation of hook_help().
poll_view_voting in modules/poll.module
Generates the voting form for a poll.
profile_block in modules/profile.module
Implementation of hook_block().
profile_field_delete in modules/profile.module
Menu callback; deletes a field from all user profiles.
profile_help in modules/profile.module
Implementation of hook_help().
search_box in modules/search.module
Output a search form for the search block and the theme's search box.
search_form in modules/search.module
Render a search form.
search_help in modules/search.module
Implementation of hook_help().
statistics_access_log in modules/statistics.module
statistics_help in modules/statistics.module
Implementation of hook_help().
story_help in modules/story.module
Implementation of hook_help().
system_help in modules/system.module
Implementation of hook_help().
system_update_177 in database/updates.inc
system_view_general in modules/system.module
taxonomy_form_term in modules/taxonomy.module
taxonomy_form_vocabulary in modules/taxonomy.module
Display form for adding and editing vocabularies.
taxonomy_help in modules/taxonomy.module
Implementation of hook_help().
taxonomy_rss_item in modules/taxonomy.module
Provides category information for rss feeds
taxonomy_term_page in modules/taxonomy.module
Menu callback; displays all nodes associated with a term.
theme_book_navigation in modules/book.module
Prepares the links to children (TOC) and forward/backward navigation for a node presented as a book page.
theme_comment_post_forbidden in modules/comment.module
theme_forum_display in modules/forum.module
Format the forum body.
theme_image in includes/theme.inc
Return a themed image.
theme_textfield in includes/form.inc
Format a textfield.
throttle_help in modules/throttle.module
Implementation of hook_help().
tracker_help in modules/tracker.module
Implementation of hook_help().
upload_help in modules/upload.module
Implementation of hook_help().
upload_nodeapi in modules/upload.module
Implementation of hook_nodeapi().
user_admin in modules/user.module
user_block in modules/user.module
Implementation of hook_block().
user_help in modules/user.module
Implementation of hook_help().
user_login in modules/user.module
user_pass_reset in modules/user.module
Menu callback; process one time login link and redirects to the user page on success.
user_pass_reset_url in modules/user.module
user_pass_submit in modules/user.module
user_register in modules/user.module
user_register_submit in modules/user.module
user_search in modules/user.module
Implementation of hook_search().
watchdog_help in modules/watchdog.module
Implementation of hook_help().
watchdog_overview in modules/watchdog.module
Menu callback; displays a listing of log messages.
_aggregator_page_list in modules/aggregator.module
Prints an aggregator page listing a number of feed items. Various menu callbacks use this function to print their feeds.
_blogapi_get_post in modules/blogapi.module
_locale_add_language in includes/locale.inc
Helper function to add a language
_upload_form in modules/upload.module

Code

includes/common.inc, line 995

<?php
function url($path = NULL, $query = NULL, $fragment = NULL, $absolute = FALSE) {
  if (isset($fragment)) {
    $fragment = '#'. $fragment;
  }

  // Return an external link if $path contains an allowed absolute URL.
  // Only call the slow filter_xss_bad_protocol if $path contains a ':' before any / ? or #.
  $colonpos = strpos($path, ':');
  if ($colonpos !== FALSE && !preg_match('![/?#]!', substr($path, 0, $colonpos)) && filter_xss_bad_protocol($path, FALSE) == check_plain($path)) {
    // Split off the fragment
    if (strpos($path, '#') !== FALSE) {
      list($path, $old_fragment) = explode('#', $path, 2);
      if (isset($old_fragment) && !isset($fragment)) {
        $fragment = '#'. $old_fragment;
      }
    }
    // Append the query
    if (isset($query)) {
      $path .= (strpos($path, '?') !== FALSE ? '&' : '?') . $query;
    }
    // Reassemble
    return $path . $fragment;
  }

  global $base_url;
  static $script;
  static $clean_url;

  if (empty($script)) {
    // On some web servers, such as IIS, we can't omit "index.php".  So, we
    // generate "index.php?q=foo" instead of "?q=foo" on anything that is not
    // Apache.
    $script = (strpos($_SERVER['SERVER_SOFTWARE'], 'Apache') === false) ? 'index.php' : '';
  }

  // Cache the clean_url variable to improve performance.
  if (!isset($clean_url)) {
    $clean_url = (bool)variable_get('clean_url', '0');
  }

  $base = ($absolute ? $base_url . '/' : base_path());

  // The special path '<front>' links to the default front page.
  if (!empty($path) && $path != '<front>') {
    $path = drupal_get_path_alias($path);
    $path = drupal_urlencode($path);
    if (!$clean_url) {
      if (isset($query)) {
        return $base . $script .'?q='. $path .'&'. $query . $fragment;
      }
      else {
        return $base . $script .'?q='. $path . $fragment;
      }
    }
    else {
      if (isset($query)) {
        return $base . $path .'?'. $query . $fragment;
      }
      else {
        return $base . $path . $fragment;
      }
    }
  }
  else {
    if (isset($query)) {
      return $base . $script .'?'. $query . $fragment;
    }
    else {
      return $base . $fragment;
    }
  }
}
?>
Login or register to post comments
 
 

All source code and documentation on this site is released under the terms of the GNU General Public License, version 2 and later. Drupal is a registered trademark of Dries Buytaert.