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
You can try this
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: