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/content/node", or an existing URL like "http://drupal.org/". The special path '<front>' may also be given and will generate the site's base URL.

$options An associative array of additional options, with the following keys:

  • 'query' A URL-encoded query string to append to the link, or an array of query key/value-pairs without any URL-encoding.
  • 'fragment' A fragment identifier (or named anchor) to append to the link. Do not include the '#' character.
  • 'absolute' (default FALSE) 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.
  • 'alias' (default FALSE) Whether the given path is an alias already.
  • 'external' Whether the given path is an external URL.
  • 'language' An optional language object. Used to build the URL to link to and look up the proper alias for the link.
  • 'base_url' Only used internally, to modify the base URL when a language dependent URL requires so.
  • 'prefix' Only used internally, to modify the path when a language dependent URL requires so.

Return value

A string containing a URL to the given path.

▾ 138 functions call url()

aggregator_admin_settings in modules/aggregator/aggregator.admin.inc
Form builder; Configure the aggregator system.
aggregator_block in modules/aggregator/aggregator.module
Implementation of hook_block().
aggregator_form_feed in modules/aggregator/aggregator.admin.inc
Form builder; Generate a form to add/edit feed sources.
aggregator_help in modules/aggregator/aggregator.module
Implementation of hook_help().
aggregator_page_categories in modules/aggregator/aggregator.pages.inc
Menu callback; displays all the categories used by the aggregator.
aggregator_page_category in modules/aggregator/aggregator.pages.inc
Menu callback; displays all the items aggregated in a particular category.
aggregator_page_last in modules/aggregator/aggregator.pages.inc
Menu callback; displays the most recent items gathered from any feed.
aggregator_page_sources in modules/aggregator/aggregator.pages.inc
Menu callback; displays all the feeds used by the aggregator.
batch_process in includes/form.inc
Process the batch.
block_admin_display_form in modules/block/block.admin.inc
Generate main blocks administration form.
block_box_form in modules/block/block.module
Define the custom block form.
block_help in modules/block/block.module
Implementation of hook_help().
blogapi_blogger_get_users_blogs in modules/blogapi/blogapi.module
blogapi_blogger_get_user_info in modules/blogapi/blogapi.module
Blogging API callback. Returns profile information about a user.
blogapi_help in modules/blogapi/blogapi.module
Implementation of hook_help().
blogapi_init in modules/blogapi/blogapi.module
blogapi_rsd in modules/blogapi/blogapi.module
blogapi_update_6000 in modules/blogapi/blogapi.install
Inform users about the new permission.
blog_block in modules/blog/blog.module
Implementation of hook_block().
blog_feed_last in modules/blog/blog.pages.inc
Menu callback; displays an RSS feed containing recent blog entries of all users.
blog_feed_user in modules/blog/blog.pages.inc
Menu callback; displays an RSS feed containing recent blog entries of a given user.
blog_help in modules/blog/blog.module
Implementation of hook_help().
blog_page_last in modules/blog/blog.pages.inc
Menu callback; displays a Drupal page containing recent blog entries of all users.
blog_page_user in modules/blog/blog.pages.inc
Menu callback; displays a Drupal page containing recent blog entries of a given user.
book_help in modules/book/book.module
Implementation of hook_help().
chameleon_page in themes/chameleon/chameleon.theme
color_form_alter in modules/color/color.module
Implementation of hook_form_alter().
color_help in modules/color/color.module
Implementation of hook_help().
comment_form in modules/comment/comment.module
Generate the basic commenting form, for appending to a node or display on a separate page.
comment_form_alter in modules/comment/comment.module
Implementation of hook_form_alter().
comment_help in modules/comment/comment.module
Implementation of hook_help().
comment_nodeapi in modules/comment/comment.module
Implementation of hook_nodeapi().
contact_admin_settings in modules/contact/contact.admin.inc
contact_help in modules/contact/contact.module
Implementation of hook_help().
contact_mail in modules/contact/contact.module
Implementation of hook_mail().
contact_mail_page in modules/contact/contact.pages.inc
contact_user in modules/contact/contact.module
Implementation of hook_user().
contact_user_page in modules/contact/contact.pages.inc
Personal contact page.
dblog_admin_settings in modules/dblog/dblog.admin.inc
dblog module settings form.
drupal_goto in includes/common.inc
Send the user to a different Drupal page.
file_create_url in includes/file.inc
Create the download path to a file.
filter_help in modules/filter/filter.module
Implementation of hook_help().
form_expand_ahah in includes/form.inc
Add AHAH information about a form element to the page to communicate with javascript. If #ahah[path] is set on an element, this additional javascript is added to the page header to attach the AHAH behaviors. See ahah.js for more information.
forum_block in modules/forum/forum.module
Implementation of hook_block().
forum_confirm_delete in modules/forum/forum.admin.inc
Returns a confirmation page for deleting a forum taxonomy term.
forum_help in modules/forum/forum.module
Implementation of hook_help().
forum_overview in modules/forum/forum.admin.inc
Returns an overview list of existing forums and containers
hook_help in developer/hooks/core.php
Provide online user help.
hook_mail in developer/hooks/core.php
Prepare a message based on parameters; called from drupal_mail().
hook_ping in developer/hooks/core.php
Ping another server.
hook_requirements in developer/hooks/install.php
Check installation requirements and do status reporting.
hook_search in developer/hooks/core.php
Define a custom search routine.
l in includes/common.inc
Format an internal Drupal link.
locale_form_alter in modules/locale/locale.module
Implementation of hook_form_alter(). Adds language fields to forms.
locale_help in modules/locale/locale.module
Implementation of hook_help().
locale_languages_predefined_form_submit in includes/locale.inc
Process the language addition form submission.
menu_get_active_help in includes/menu.inc
Returns the help associated with the active menu item.
menu_help in modules/menu/menu.module
Implementation of hook_help().
node_block in modules/node/node.module
Implementation of hook_block().
node_feed in modules/node/node.module
A generic function for generating RSS feeds from a set of nodes.
node_help in modules/node/node.module
Implementation of hook_help().
node_page_default in modules/node/node.module
Menu callback; Generate a listing of promoted nodes.
node_search in modules/node/node.module
Implementation of hook_search().
openid_authentication in modules/openid/openid.module
Authenticate a user or attempt registration.
openid_authentication_request in modules/openid/openid.module
openid_form_alter in modules/openid/openid.module
Implementation of hook_form_alter : adds OpenID login to the login forms.
openid_help in modules/openid/openid.module
Implementation of hook_help().
openid_login_validate in modules/openid/openid.module
Login form _validate hook
openid_user_add_submit in modules/openid/openid.pages.inc
path_admin_form in modules/path/path.admin.inc
Return a form for editing or creating an individual URL alias.
php_filter_tips in modules/php/php.module
Implementation of hook_filter_tips().
php_help in modules/php/php.module
Implementation of hook_help().
ping_help in modules/ping/ping.module
Implementation of hook_help().
poll_help in modules/poll/poll.module
Implementation of hook_help().
profile_block in modules/profile/profile.module
Implementation of hook_block().
profile_field_delete in modules/profile/profile.admin.inc
Menu callback; deletes a field from all user profiles.
search_admin_settings in modules/search/search.admin.inc
Menu callback; displays the search module settings page.
search_form in modules/search/search.module
Render a search form.
search_help in modules/search/search.module
Implementation of hook_help().
statistics_access_log in modules/statistics/statistics.admin.inc
Menu callback; Displays recent page accesses.
statistics_access_logging_settings in modules/statistics/statistics.admin.inc
Form builder; Configure access logging.
statistics_help in modules/statistics/statistics.module
Implementation of hook_help().
syslog_admin_settings in modules/syslog/syslog.module
syslog_help in modules/syslog/syslog.module
Implementation of hook_help().
system_check_http_request in modules/system/system.module
Checks whether the server is capable of issuing HTTP requests.
system_date_time_settings in modules/system/system.admin.inc
Form builder; Configure the site date and time settings.
system_error_reporting_settings in modules/system/system.admin.inc
Form builder; Configure error reporting settings.
system_help in modules/system/system.module
Implementation of hook_help().
system_mail in modules/system/system.module
Implementation of hook_mail().
system_main_admin_page in modules/system/system.admin.inc
Menu callback; Provide the administration overview page.
system_message_action in modules/system/system.module
A configurable Drupal action. Sends a message to the current user's screen.
system_modules in modules/system/system.admin.inc
Menu callback; provides module enable/disable interface.
system_modules_uninstall in modules/system/system.admin.inc
Builds a form of currently disabled modules.
system_requirements in modules/system/system.install
Test and report Drupal installation requirements.
system_site_information_settings in modules/system/system.admin.inc
Form builder; The general site information form.
system_site_maintenance_settings in modules/system/system.admin.inc
Form builder; Configure the site's maintenance status.
system_themes_form_submit in modules/system/system.admin.inc
Process system_themes_form form submissions.
system_update_6017 in modules/system/system.install
Rename settings related to user.module email notifications.
system_update_6026 in modules/system/system.install
Display warning about new Update status module.
system_update_6045 in modules/system/system.install
Update blog, book and locale module permissions.
taxonomy_rss_item in modules/taxonomy/taxonomy.module
Provides category information for RSS feeds.
taxonomy_term_page in modules/taxonomy/taxonomy.pages.inc
Menu callback; displays all nodes associated with a term.
template_preprocess_aggregator_feed_source in modules/aggregator/aggregator.pages.inc
Process variables for aggregator-feed-source.tpl.php.
template_preprocess_aggregator_item in modules/aggregator/aggregator.pages.inc
Process variables for aggregator-item.tpl.php.
template_preprocess_book_navigation in modules/book/book.module
Process variables for book-navigation.tpl.php.
template_preprocess_forums in modules/forum/forum.module
Process variables for forums.tpl.php
template_preprocess_forum_list in modules/forum/forum.module
Process variables to format a forum listing.
template_preprocess_forum_topic_list in modules/forum/forum.module
Preprocess variables to format the topic listing.
template_preprocess_forum_topic_navigation in modules/forum/forum.module
Preprocess variables to format the next/previous forum topic navigation links.
template_preprocess_node in includes/theme.inc
Process variables for node.tpl.php
template_preprocess_page in includes/theme.inc
Process variables for page.tpl.php
theme_aggregator_page_rss in modules/aggregator/aggregator.pages.inc
Theme the RSS output.
theme_comment_post_forbidden in modules/comment/comment.module
Theme a "you can't post comments" notice.
theme_image in includes/theme.inc
Return a themed image.
theme_textfield in includes/form.inc
Format a textfield.
translation_form_alter in modules/translation/translation.module
Implementation of hook_form_alter().
translation_help in modules/translation/translation.module
Implementation of hook_help().
trigger_help in modules/trigger/trigger.module
Implementation of hook_help().
update_help in modules/update/update.module
Implementation of hook_help().
update_mail in modules/update/update.module
Implementation of hook_mail().
update_settings in modules/update/update.settings.inc
Form builder for the update settings tab.
upload_admin_settings in modules/upload/upload.admin.inc
Menu callback for the upload settings form.
upload_form_alter in modules/upload/upload.module
upload_help in modules/upload/upload.module
Implementation of hook_help().
user_help in modules/user/user.module
Implementation of hook_help().
user_login_block in modules/user/user.module
user_login_final_validate in modules/user/user.module
A validate handler on the login form. Should be the last validator. Sets an error if user has not been authenticated yet.
user_mail_tokens in modules/user/user.module
Return an array of token to value mappings for user e-mail messages.
user_pass_reset in modules/user/user.pages.inc
Menu callback; process one time login link and redirects to the user page on success.
user_pass_reset_url in modules/user/user.module
user_register_submit in modules/user/user.module
Submit handler for the user registration form.
user_search in modules/user/user.module
Implementation of hook_search().
_batch_progress_page_js in includes/batch.inc
Batch processing page with JavaScript support.
_batch_progress_page_nojs in includes/batch.inc
Batch processing page without JavaScript support.
_blogapi_get_post in modules/blogapi/blogapi.module
_update_message_text in modules/update/update.module
Helper function to return the appropriate message text when the site is out of date or missing a security update.
_update_no_data in modules/update/update.module
Prints a warning message when there is no data about available updates.
_user_edit_validate in modules/user/user.module

Code

includes/common.inc, line 1407

<?php
function url($path = NULL, $options = array()) {
  // Merge in defaults.
  $options += array(
    'fragment' => '',
    'query' => '',
    'absolute' => FALSE,
    'alias' => FALSE,
    'prefix' => ''
  );
  if (!isset($options['external'])) {
    // 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, ':');
    $options['external'] = ($colonpos !== FALSE && !preg_match('![/?#]!', substr($path, 0, $colonpos)) && filter_xss_bad_protocol($path, FALSE) == check_plain($path));
  }

  // May need language dependent rewriting if language.inc is present.
  if (function_exists('language_url_rewrite')) {
    language_url_rewrite($path, $options);
  }
  if ($options['fragment']) {
    $options['fragment'] = '#'. $options['fragment'];
  }
  if (is_array($options['query'])) {
    $options['query'] = drupal_query_string_encode($options['query']);
  }

  if ($options['external']) {
    // Split off the fragment.
    if (strpos($path, '#') !== FALSE) {
      list($path, $old_fragment) = explode('#', $path, 2);
      if (isset($old_fragment) && !$options['fragment']) {
        $options['fragment'] = '#'. $old_fragment;
      }
    }
    // Append the query.
    if ($options['query']) {
      $path .= (strpos($path, '?') !== FALSE ? '&' : '?') . $options['query'];
    }
    // Reassemble.
    return $path . $options['fragment'];
  }

  global $base_url;
  static $script;

  if (!isset($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' : '';
  }

  if (!isset($options['base_url'])) {
    // The base_url might be rewritten from the language rewrite in domain mode.
    $options['base_url'] = $base_url;
  }

  // Preserve the original path before aliasing.
  $original_path = $path;

  // The special path '<front>' links to the default front page.
  if ($path == '<front>') {
    $path = '';
  }
  elseif (!empty($path) && !$options['alias']) {
    $path = drupal_get_path_alias($path, isset($options['language']) ? $options['language']->language : '');
  }

  if (function_exists('custom_url_rewrite_outbound')) {
    // Modules may alter outbound links by reference.
    custom_url_rewrite_outbound($path, $options, $original_path);
  }

  $base = $options['absolute'] ? $options['base_url'] .'/' : base_path();
  $prefix = empty($path) ? rtrim($options['prefix'], '/') : $options['prefix'];
  $path = drupal_urlencode($prefix . $path);

  if (variable_get('clean_url', '0')) {
    // With Clean URLs.
    if ($options['query']) {
      return $base . $path .'?'. $options['query'] . $options['fragment'];
    }
    else {
      return $base . $path . $options['fragment'];
    }
  }
  else {
    // Without Clean URLs.
    $variables = array();
    if (!empty($path)) {
      $variables[] = 'q='. $path;
    }
    if (!empty($options['query'])) {
      $variables[] = $options['query'];
    }
    if ($query = join('&', $variables)) {
      return $base . $script .'?'. $query . $options['fragment'];
    }
    else {
      return $base . $options['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.