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
LVL 1
Ensemble TravelAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jason C. LevineNo oneCommented:
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.
0
gheistCommented:
It is much safer to store such attributes in session variables so that user cannot mess them up.
0
Ensemble TravelAuthor Commented:
@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?
0
Jason C. LevineNo oneCommented:
Well, as gheist says, if that's the goal then you are better off storing it in a session and just outputting it where it needs to be.  That's a helluva lot easier to maintain than constantly having to append the query string no matter what the user does on the site plus all of the overhead involved in sanitizing inputs.

However, if you want to do it that way then you would need to hook into the Rewrite API and use some combination of add_rewrite_rule() and add_rewrite_tag() functions to globally rewrite all URLs with the query strings. Better you than me, though.

ref:

http://codex.wordpress.org/Rewrite_API
http://codex.wordpress.org/Rewrite_API/add_rewrite_rule
http://codex.wordpress.org/Rewrite_API/add_rewrite_tag

This is a snippet of how to use the functions:

/* Aesthetically Pleasing (Pretty) Permalinks
-------------------------------------------------------*/
add_action( "init", "pleasing_permalinks" );
function pleasing_permalinks() {
	//register custom query variables
	add_rewrite_tag( '%hash%', '([^&]+)' );
	add_rewrite_tag( '%action%', '([^&]+)' );

	//Faculty management urls
	add_rewrite_rule(
		'^meetings/faculty/([^/]+)/?$',
		'index.php?pagename=meetings/faculty&hash=$matches[1]',
		'top'
	);
}

Open in new window


taken from one of my production sites.  Please note that we use it to do targeted redirections and convert domain.com/?x=y to a nicer domain.com/y/ format for single-use pages and we don't globally rewrite all links but the concept is the same.  You would just have to be careful that you don't screw up other plugins or even the base WordPress rewriting functions.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Sam CohenConsultant to Digital/DeveloperCommented:
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!!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.