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.
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.
Share Improve this question edited Jun 7, 2019 at 0:03 fuxia♦ 107k39 gold badges255 silver badges459 bronze badges asked Jun 6, 2019 at 18:59 cemondcemond 13 Answers
Reset to default 1It would be best to include this in your link. For example, the link to your form could be https://domains/form/?product=20
. That way the product ID will be available as as GET parameter, eg $_GET['product']
.
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 )
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.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745428586a4627290.html
评论列表(0条)