最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to get WordPress post ID of last visited page?

programmeradmin2浏览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.

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 1
Add a comment  | 

3 Answers 3

Reset to default 1

It 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.

发布评论

评论列表(0)

  1. 暂无评论