How to Add query parameter from url to all links on the page? Basically I want to get the Query parameter from url and then attach that on all the links on the page. JQuery or PHP, Any solutions?

Please suggest. Thanks in Advance!


You can do it easily in a custom module, implementing hook_url_outbound_alter().

function mymodule_url_outbound_alter(&$path, &$options, $original_path) {
  $param = $_GET['q'];
  $path = 'build-path-as-desired.';

That should do it for you. The hook serves, basically, to alter outbound urls. You can look at the documentation for it in the system.api.php file, and I suggest you do that, as there are a couple of things to take into account when altering urls through it, but it's definitely an easy way of doing what you want to do.

EDIT: Online documentation for the hook - https://api.drupal.org/api/drupal/modules%21system%21system.api.php/function/hook_url_outbound_alter/7

EDIT 2: Additional functions to work with the path being requested: drupal_get_query_parameters() and drupal_get_path_alias($path).

  • I would rather use something different from $_GET['q'] as example, since that is the internal path used by Drupal. – kiamlaluno Apr 26 '15 at 16:34
  • Sure, that makes sense. I should have mentioned I added the "$_GET['q']" just as a quick example of how to get the current query, but you don't necessarily have to use it. I've edited the response to add other functions you can use to retrieve info about the requested URL (such as the path alias, instead of the internal drupal path). – Salva Molina Apr 27 '15 at 6:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.