To encode the URLs on the source page, in this case ‘home’:
function append_query_string( $url, $post, $leavename ) {
if ( $post->post_type == 'post' ) {
$url = add_query_arg( 'source', 'home', $url );
}
return $url;
}
add_filter( 'post_link', 'append_query_string', 10, 3 );
This adds ?source=home
to the end of post link URLs on the home page.
To decode the URL on the target page ie. create a variable with ‘home’ detected as its value in this example:
$posturl = $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"];
parse_str( parse_url( $posturl, PHP_URL_QUERY ), $get_args);
$source = $get_args['source'];
Thanks to Laxmana for the suggestion.