I am trying to get the ID of a referring WordPress page and show link based on the specific previous page. I know that we can use server http referrer but it would be more suitable for me to get the referring WP page/post ID and echo some html content on the page by if statement.

I have a following code example put together from various parts (it is not structurally correct but I hope you got the idea):

$visitorcamefrom = $_SERVER['HTTP_REFERER'];
if ( $visitorcamefrom == icl_object_id(17, 'page', true) ) { 
    echo <a href="<?php echo(get_permalink(icl_object_id(8, 'page', true))); ?>" class="absolute-top-left left-arrow btn btn-lg btn-primary visible-lg"><span class="icon1"></span></a> 
}else { 
    echo <a href="<?php echo(get_permalink(icl_object_id(12, 'page', true))); ?>" class="absolute-top-left left-arrow btn btn-lg btn-primary visible-lg"><span class="icon2"></span></a> 
};

1 Answers

0
Community On
  $visitorcamefrom = url_to_postid( wp_get_referer() );

wp_get_referer() will return the url of the referring page and then url_to_postid() will retrieve the ID of that url.

I fast tested it and seems to work well. Hope this helps!