I am using wp_redirect ( get_permalink ( $post->ID ) ); to redirect users to variable (random) posts I need to know if the user redirected to current post from specific URL or not

Is it possible to store the current URL before redirecting then pass it to the redirected post

what I need to do is

if user  reach this page from www.example.com/?p=123 
// do some thing

// another staff

I tried to use wp_get_referer(); but it's not what I need


How about adding parameters to the URL you redirect to:

wp_redirect ( add_query_arg("utm_source", $thispage, get_permalink ( $post->ID ) ));

and then checking the parameter on the target page?

  • Good Idea , but I don't need it to be parameter so I tried to add anchor but unfortunately PHP don't fetch any thing after # , BTW your code working fine – adnan Oct 20 '17 at 6:43
  • Yeah, the hash part of any URL ("#...") is client side only. How about setting a cookie with the path set to the target page (to help avoid race conditions)? – janh Oct 20 '17 at 6:49
  • this is exactly my question, but I have no Idea about how to do it – adnan Oct 20 '17 at 7:11
  • setcookie("origin", $thispageId, 0, preg_replace("!^https?://[^/]+!", "", get_permalink($thatpage))); before the redirect, then look at $_COOKIE["origin"]. This won't play nice with caching, but if that's not a problem, this should work just fine. – janh Oct 20 '17 at 7:19
  • @adnan What’s wrong with a parameter? This sort of thing is what they’re for – Jacob Peattie Oct 20 '17 at 7:23

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.