I have comments enabled on different types of pages in Wordpress (archive, tag, search, main page), and after a user posts a comment I would like them to be redirected back to their referring page, not to the single post page. Any ideas?
Go to Tools > Redirection and scroll down to the Add new redirection section. In the Source URL field, type or paste in the URL you want to redirect from. In the Target URL field, type or paste in the URL you want to redirect to.
Go to 'Settings' in the WordPress admin menu and then click on 'Website Redirect'. Enter the URL you want to redirect the site to, set the desired redirection type, set the status to 'Enabled' and save your changes!
By default Redirection will manage all redirects using WordPress. However you can configure it so redirects are automatically saved to a . htaccess file and handled by Apache itself. If you use Nginx then you can export redirects to an Nginx rewrite rules file.
Go to WP-Admin Dashboard > Settings > 301 Redirects > Enter the old URL under 'Requests' and the new URL under 'Destination' > Save Changes.
I would advise against returning $_SERVER["HTTP_REFERER"]
as it's not reliable.
The address of the page (if any) which referred the user agent to the current page. This is set by the user agent. Not all user agents will set this, and some provide the ability to modify HTTP_REFERER as a feature. In short, it cannot really be trusted.
Source https://php.net/manual/en/reserved.variables.server.php
Here's an alternative
add_filter( 'comment_post_redirect', function ( $location ) {
return get_permalink($_POST['comment_post_ID']);
} );
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With