Link to home
Start Free TrialLog in
Avatar of Ensemble Travel
Ensemble Travel

asked on

Maintain Parameters on URL throughout Site (PHP)

Purpose:

I am attempting to maintain the parameter key and value in the URL after it has been appended. The reason I need this is to maintain URL integrity for social sharing, so that they are properly referred to the correct URL that has been shared.

Parameter Key = '?specialst='
Parameter Value = 'name-of-person'

If this parameter is set, a $_SESSION is set which replaces broad conversion funnels to individual specialist contact information.

Problem

How can I force this parameter to append itself to all URLS throughout the website? On entrance to the website, the parameter can be added to the URL but as you navigate through the website the parameter disappears immediately.

The back-end system for the website is Wordpress - this may help or hinder in my solution below.

I haven't been able to find any concrete solutions or concepts online that I can clearly understand.

My (Attempted) Solution

Below, I am checking to see if the parameter is in the URL via a $_SESSION storage and recall check. If it is set, I am rebuilding the parameter and the output is specialist=name-of-person.

if($_SESSION['specialist_id'] != '') {
	$newurl = http_build_query(array_merge(array('specialist'=>$_SESSION{'specialist_id'})));
}

Open in new window

Where I am lost is how to reattach this output to the end of the URL.

For example:

test.com/happy/?specialist=name-of-person
test.com/happy/super/great/?specialist=name-of-person
Avatar of Jason C. Levine
Jason C. Levine
Flag of United States of America image

If you are using WordPress, then you should probably not use http_build_query() and instead use the WordPress function add_query_arg() instead:

http://codex.wordpress.org/Function_Reference/add_query_arg

There are code samples there that will show you how to use it but that will allow to take any variables you have and output a URL with the query strings formatted.
It is much safer to store such attributes in session variables so that user cannot mess them up.
Avatar of Ensemble Travel
Ensemble Travel

ASKER

@Jason - How do I actually ensure that the parameter stays appended to the URL though?

For example if I want ?test=test to stay on all URLs throughout navigation?
ASKER CERTIFIED SOLUTION
Avatar of Jason C. Levine
Jason C. Levine
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Here some wordpress info: http://codex.wordpress.org/Function_Reference/add_query_arg

Try this plugin:
https://wordpress.org/plugins/yd-spread-parameter/

OR

Simply add this to you function.php file:


function add_my_query_var( $link ) {

$paramKey = 'specialist';
$paramValue = 'John'; //<-- This can be dynamic or the current user logged in. etc.

    $link = add_query_arg( $paramKey, $paramValue, $link );
    return $link;
}
//Filter to add to every link in wordpress
add_filter('page_link','add_my_query_var');
add_filter('post_link','add_my_query_var');
add_filter('term_link','add_my_query_var');
add_filter('tag_link','add_my_query_var');
add_filter('category_link','add_my_query_var');
add_filter('post_type_link','add_my_query_var');
add_filter('attachment_link','add_my_query_var');
add_filter('year_link','add_my_query_var');
add_filter('month_link','add_my_query_var');
add_filter('day_link','add_my_query_var');
add_filter('search_link','add_my_query_var');

add_filter('feed_link','add_my_query_var');
add_filter('post_comments_feed_link','add_my_query_var');
add_filter('author_feed_link','add_my_query_var');
add_filter('category_feed_link','add_my_query_var');
add_filter('taxonomy_feed_link','add_my_query_var');
add_filter('search_feed_link','add_my_query_var');

add_filter('get_edit_tag_link','add_my_query_var');
add_filter('get_edit_post_link','add_my_query_var');
add_filter('get_delete_post_link','add_my_query_var');
add_filter('get_edit_comment_link','add_my_query_var');
add_filter('get_edit_bookmark_link','add_my_query_var');

add_filter('index_rel_link','add_my_query_var');
add_filter('parent_post_rel_link','add_my_query_var');
add_filter('previous_post_rel_link','add_my_query_var');
add_filter('next_post_rel_link','add_my_query_var');
add_filter('start_post_rel_link','add_my_query_var');
add_filter('end_post_rel_link','add_my_query_var');

add_filter('previous_post_link','add_my_query_var');
add_filter('next_post_link','add_my_query_var');

add_filter('get_pagenum_link','add_my_query_var');
add_filter('get_comments_pagenum_link','add_my_query_var');
add_filter('shortcut_link','add_my_query_var');
add_filter('get_shortlink','add_my_query_var');

////Left these commented out because it will affect you admin url etc
//add_filter('home_url','add_my_query_var');
//add_filter('site_url','add_my_query_var');
//add_filter('admin_url','add_my_query_var');
//add_filter('includes_url','add_my_query_var');
//add_filter('content_url','add_my_query_var');
//add_filter('plugins_url','add_my_query_var');

//add_filter('network_site_url','add_my_query_var');
//add_filter('network_home_url','add_my_query_var');
//add_filter('network_admin_url','add_my_query_var');

Open in new window



Take care! Let me know if that works!!