skip to Main Content

I use wordpress.
When a user requests or searches for a page that does not exist on the site, they are referred to a 404 page.

I have created a button to return to the main page using the following shortcode:

<a href="<?php echo home_url() . '/'; ?>">Go to home page</a>

But I don’t want to refer the user to the main page of the site when he goes to the 404 page, but I want to refer it to the previous page using a short code.

Is there a shortcode like the one above so I don’t have to refer the user to the homepage?

2

Answers


  1. You can try this

    <a href="javascript:history.go(-1)">Go to home page</a>
    
    Login or Signup to reply.
  2. You can use the wp_get_referer() function to get the URL of the previous page that the user visited, and then use that URL in your shortcode. Here’s an example of how you can do this:

    <?php
    $referer = wp_get_referer();
    if ($referer == get_permalink() || $referer == '') {
        $referer = home_url() . '/';
    }
    ?>
    
    <a href="<?php echo $referer; ?>">Go to home page</a>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search