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;
}

17 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
  8. Often, this isn't necessary, but it also means that any plugins and themes (templates) created for the platform are also open source! Imagine a world-wide community working together to add more features and functionality to WordPress - you're not limited to only being able to use specific employees or developers to produce customizations, which means it's a powerful CMS system ready to do what you need it to, it's flexible, and easy to customize.https://medijo.lt/etiketes/

    ReplyDelete
  9. This is such a great resource that you are providing and you give it away for free. hosting companies in south africa

    ReplyDelete
  10. Next you will need to get hosting for your website. I would recommend something from Godaddy or 1and1 because they have good customer service and give you a free domain or free hosting with the purchase of one or the other. managed wordpress services

    ReplyDelete
  11. WordPress likewise allows you to get an expert plan to make your site look extraordinary - frequently for nothing out of pocket. neuronto deepl wordpress plugin

    ReplyDelete
  12. Its an incredible joy perusing your post.Its brimming with data I am searching for and I want to post a remark that "The substance of your post is magnificent" Great work.  ranking

    ReplyDelete
  13. Nice collection. keep posting such things. Very useful information. Thank you. we also provide best service for Greengeeks Review in USA with money back guarantee. if you want more information then you can visit on our website.

    ReplyDelete
  14. xn-pick casino【WG】best online casino,trulette
    【n-pick casino】,【WG98.vip】⚡,best online casino,trulette,trulette,free poker online,trulette,mister bonus no deposit,vip 메리트 카지노 주소 slot machine

    ReplyDelete
  15. Hello Everyone !

    Offering fresh & Valid Tools with Tutorials & Fullz/Pros/Leads.
    All stuff will be genuine, Guaranteed & verified.
    Cheap prices & Fastest Delivery

    For more details :
    WA - +92-317-2721122
    TG/ICQ - @killhacks
    Email - exploit(dot)tools4u at Gmail(dot)com
    Wickr/Skype - @peeterhacks

    Contact us for any query 24/7
    Huge stuff we are offering

    SSN+DOB+DL Fullz with Complete Info
    High-Credit-Scores-Pros/Fullz
    Dumps-With-Pins Track-101&202
    C.C Fullz with C.V.V & SSN Info
    Business E I N Fullz
    Office365 Logs/Leads
    Hac-king Tools, Tutorials, Ebooks & Easy Video Tuts
    Complete package of SPA-MMING with all necessary gadgets
    Loan & Car-ding Methods/Tutorials/Cash out/Transfers
    2021/2022 Fr**d B**le updated
    Cracker&Flasher For BTC
    RDP's-SMTP's-C-panels-Shells
    Web-mailers-Brutes-Crackers
    SQLi-Injector/Server-Penetration
    Kali-Linux-Master-Class Full Package
    Premium-Logs (Amazon, E-bay, Coinbase, Netflix)
    Logs/I.P's/Proxies/Server I.P's

    Just-contact-us-for-your-desired-stuff
    We will try our best to provide you Legit & Genuine Stuff

    TG/ICQ - @killhacks
    Wickr/Skype - @peeterhacks
    WA - +92-317-2721122
    Email - exploit(dot)tools4u at Gmail(dot)com

    ReplyDelete