Smart "Back" Button for Wordpress posts/pages

While coding a Wordpress site, I wanted to add a back button for posts and pages.
Hierarchical pages should obviously point back to their parents, but what of non hierarchical pages?

If a user navigated from the "Gallery" page to the "About" page - I would like the sites "back" button to return him to the "Gallery" page. This could be easily implemented using JavaScript but why use user-side scripting when you can generate the code on the server side?

The obvious solution would be to use the referring URL as a target for our "back" button. We must now avoid two special cases:

  1. The page is a sub-page and the back button should point to its parent.
  2. The previous URL was an off-site URL, and the back button should not appear at all.
This code snippet solves both problems: if the post is non-hierarchical, the referring URL is checked - if no URL is provided, or if the URL is off-site, no "back" button is displayed.
$ref_url = wp_get_referer();
$ref_parse = parse_url($ref_url);
$my_parse = parse_url(get_permalink());
if (($ref_url==get_permalink() || empty($ref_url)) && $post->post_parent) {
 $show_back_to_parent=true;
 $ref_url = get_permalink($post->post_parent);
}
if ($ref_parse[host]==$my_parse[host] || $show_back_to_parent) { 
 echo $ref_url;
}

8 comments:

  1. Hi, this is exactly what I need but how would this be implemented? I'm a complete novice.

    ReplyDelete
  2. Hello,

    Great idea!!!

    I have one question. How to display button for off-site URL?

    ReplyDelete
  3. Once installed and running you'll have a WAMP icon down in the system tray in the bottom right-hand corner of your screen (it looks like a semi-circle)click the WAMP icon down in the system tray and select phpMyAdmin - if the phpMyAdmin page displays then this tells us that the webserver is running and the php script can connect to our MySQL database so you're up and running.https://edkentmedia.com/website-design-toronto

    ReplyDelete
  4. This comment has been removed by the author.

    ReplyDelete
  5. Drivers for it, you can download here: https://huaweidrivers.com/

    ReplyDelete
  6. I like survey objectives which comprehend the cost of passing on the astounding solid resource purposeless out of pocket. I really worshiped inspecting your posting. Thankful to you!   seo services calgary

    ReplyDelete
  7. Great client service is moving above and over and above of responsibility. Moving above and beyond only helping someone. That modest extra mile. It is going far above what your usual job duties are. Whatever they're looking to receive as service as towards service or their product, you exceed to allow it to happen to get your own client. And it's really more than simply providing the support, it's genuinely believing in your product or your ceremony and helping the customer in order that they see that the value from that too. You can find Details on This Issue on This Sitehttps://servicecenterguru.com

    ReplyDelete