How to get WordPress post ID of last visited page?

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745428586a4627290.html

相关推荐

  • How to get WordPress post ID of last visited page?

    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.

    6小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信