Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In wordpress, how to redirect after a comment back to the referring page?

Tags:

php

wordpress

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?

like image 820
Scott Chandler Avatar asked Dec 26 '10 17:12

Scott Chandler


People also ask

How do I redirect a post to a page in WordPress?

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.

How do I automatically redirect a page in WordPress?

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!

Does WordPress redirect automatically?

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.

How do I redirect an old link to a new link in WordPress?

Go to WP-Admin Dashboard > Settings > 301 Redirects > Enter the old URL under 'Requests' and the new URL under 'Destination' > Save Changes.


1 Answers

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']);
} );
like image 57
timofey.com Avatar answered Nov 15 '22 14:11

timofey.com