0

I need to get the post ID of the last visited page.

I am creating a Get a Quote form for products listed on the website. I'd like the form page to have a heading "Quote for PRODUCT X". Product X being the product the user was just viewing.

If I can get the post ID of the last page I should then be able to pull in the post title.

1

It would be best to include this in your link. For example, the link to your form could be https://domains.com/form/?product=20. That way the product ID will be available as as GET parameter, eg $_GET['product'].

0

To get last-visited page URL

$last_visited_url = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '';

to get post id, you can use url_to_postid( string $url ) this function.

$last_visited_url_post_id = url_to_post_id( $last_visited_url )
0

The $_SERVER['HTTP_REFERER'] isn't always that reliable as it depends on the HTTP Referer header being sent by the user's browser, so it may not always exist or could even be incorrect.

MikeNGarret's solution of sending it in the url to the form would be a much more reliable way of doing it.

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.