1

So I have a URL which has a tracking link applied to the end. For some reason WordPress is removing the & which is causing the tracking to fail. You can see the URL example here:

http://www.domain.co.uk/internalpage/?&mkwid=smWfvaLGf_dm

When you go through this, you then get redirected in a way to:

http://www.domain.co.uk/internalpage/?mkwid=smWfvaLGf_dm

Note the & has been removed. Also, this only happens on internal pages. Does anyone know how to stop this?

  • Just curious, why do you need the extra & part and what tracking is failing? I guess I'm misunderstanding something. – birgire Feb 3 '16 at 11:52
  • Essential it is best practice to keep in the "&" as Marin (PPC Optimisation Software) builds their urls (The tracking element) a certain way. The tracking is failing because with the "&" being removed it is also stripping out the gclid (Google Analytics Click ID) which means that Google cannot tell if the traffic is Paid Search, therefore allocates it as "Direct" which is messing up GA essentially. – Coder_2016 Feb 3 '16 at 12:18
  • It looks like this replacement takes place in the huge redirect_canonical() callback, where it removes only the first & so ?&a=1&b=2 is redirected to ?a=1&b=2 – birgire Feb 3 '16 at 12:24
1

Here's the why part:

This part of the redirect_canonical() is removing the leading & in the redirect query part:

// tack on any additional query vars
$redirect['query'] = preg_replace( '#^\??&*?#', '', $redirect['query'] );

Example:

example.tld/?&a=1&b=2&c=3 

is redirected to

example.tld/?a=1&b=2&c=3 

If you must have the leading & you might try to adjust it through the redirect_canonical filter:

/**
 * Filter the canonical redirect URL.
 *
 * Returning false to this filter will cancel the redirect.
 *
 * @since 2.3.0
 *
 * @param string $redirect_url  The redirect URL.
 * @param string $requested_url The requested URL.
 */
$redirect_url = apply_filters( 'redirect_canonical', $redirect_url, $requested_url );
  • OK this seems very interesting! Do you know how I would adjust the redirect_canonical in order to keep my leading "&" ? Also with every core wordpress update this change would get removed right? I only ask because there must be a better solution from Marin if this is the case. As if this is the way WordPress is built, there must be a reason. Thanks for the above :) – Coder_2016 Feb 3 '16 at 12:55
  • Hey - so I have found a semi-solution. The reason why Marin needs the "&" is because it doesn't know where in the tracking parameter it is going to be placed. So say we have other tracking ID's like the below: example.tld/?&a=1&other_tracking&marin_tracking In order not to break it, it always attaches the "&" otherwise it might end up like this example.tld/?&a=1&other_trackingmarin_tracking So if Marin is first in the URL we can remove the "&" from being placed in which fixes this. However it does mean it is open to breaking in the order of the tracking is changed. – Coder_2016 Feb 3 '16 at 13:03
  • Don't modify the core files. Do it through the filter within your custom plugin. You could try to see if the diff of $redirect_url and $requested_url is only & and then return false to stop the redirection. @Coder_2016 – birgire Feb 3 '16 at 13:04
  • If you do have another solution that would be great though - Thanks! – Coder_2016 Feb 3 '16 at 13:06
0

You can use here rawurlencode() function. First pass "?&mkwid=smWfvaLGf_dm" this url into the function then you will get "%3F%26mkwid%3DsmWfvaLGf_dm" encoded value.

Now go on the functions.php and put this code there (But this code will run on every call of you site)

//for accessing current url
$accessUrl = get_site_url() .$_SERVER['REQUEST_URI'];

$pos = strpos($accessUrl, "%");
if ($pos !== false) 
{
    $accessString = rawurldecode($accessUrl);
    wp_redirect($accessString);
    exit();
}

I feel this will help you. Thanks

  • With this solution though, do I have to enter in the URL in the Raw URL encode function each time? I can't use this because there are 100's of different tracking parameters so I would have to store each of these. A live example can be seen here on WordPress actually: wordpress.org/showcase/?&mkwid=smWfvaLGf_dm – Coder_2016 Feb 3 '16 at 12:44

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.