\n \n\n
function redirect_after_comment(){\nwp_redirect('\/thank-you\/');\nexit();\n}\nadd_filter('comment_post_redirect', \n'redirect_after_comment');<\/pre>\n\n<\/div>\n\n\n<\/pre>\n\n\n\n\nChange the name of the page on the 2nd<\/sup> line<\/li>\n\n\n\nSave changes<\/li>\n<\/ol>\n\n\n\nThat\u2019s it. You can now go on and test the function. Open any of your posts and leave a comment. If you have done everything correctly, you should end up on http:\/www.yoursite.com\/thank-you page or the one specified on the second line of your code.<\/strong><\/p>\n\n\n\n
<\/figure><\/div>\n\n\n<\/p>\n\n\n\n
Don\u2019t forget to thank people for commenting but also don\u2019t forget that you want your visitors to stay on the site and continue on browsing. A good idea might be to add a list of your recent or most popular posts, or maybe even better to list all related posts which might be of interest to a commenter. If you have a mailing list, this is a good place to offer a subscription to it.<\/strong><\/p>\n\n\n\nWhile this function offers an easy and quick way of redirecting users after they leave a comment, you may be interested in a plugin that will give you some additional features.<\/p>\n\n\n\n
Redirect users after search if there was only one result found<\/h2>\n\n\n\n Search functionality is a must have if you\u2019re dealing with a larger number of articles. If you are writing on a daily basis, imagine the number of articles scattered across different categories. If you publish only three short articles per day, there will be over a thousand different titles in your database after the first year. Now imagine yourself coming to the site for the first time while looking for something specific \u2013 you will want to search for it.<\/p>\n\n\n\n
By default, WordPress will search through post titles, tags and of course the content. So, if searching for a word or a specific phrase, it will probably found dozens of posts and pages which contain the query. But what if there was only one search result?<\/p>\n\n\n\n
If someone was searching for a very specific phrase or an entire sentence, chances are there will be only a few or maybe only one result. If there is only one result found, redirecting a user to the search results page is quite unnecessary<\/strong>. Instead, why wouldn\u2019t you take that user directly to the post\/page found in the result?<\/p>\n\n\n\nLet\u2019s see how to do that:<\/p>\n\n\n\n
\nOpen functions.php<\/li>\n\n\n\n Copy this code and paste it at the end of the file:<\/li>\n<\/ol>\n\n\n\n \n\n
add_action('template_redirect', \n'redirect_search_result);\nfunction redirect_search_result() {\nif (is_search()) {\nglobal $wp_query;\nif ($wp_query->post_count == 1 && \n$wp_query->max_num_pages == 1) {\nwp_redirect( get_permalink( \n$wp_query->posts['0']->ID ) );\nexit;\n}\n}\n}<\/pre>\n\n<\/div>\n\n\n<\/pre>\n\n\n\n\nSave changes<\/li>\n<\/ol>\n\n\n\nYou have prepared your WordPress for the new functionality. After you have saved changes, every time a user searches for something which will return only one results, that user will be redirected directly to the post\/page.<\/strong><\/p>\n\n\n\nEven if it seems like nothing much, we\u2019re sure your visitors will like you for saving them a click of a button. Saving a click here and a click there will result in a better user experience and that\u2019s probably something you want on your website, isn\u2019t it?<\/p>\n\n\n\n
Redirect users when there’s only one post in a category found<\/h2>\n\n\n\n Instead of opening a category or tag page which contains only one result, you could redirect that user directly to the post\/page. As with the most of our code snippets, it will be enough just to copy and paste the code:<\/p>\n\n\n\n
Redirect a user if there was only one post in a category or a tag:<\/h3>\n\n\n\n\nOpen functions.php file<\/li>\n\n\n\n Copy and paste the following code:<\/li>\n<\/ol>\n\n\n\n \n\n
function redirect_to_post(){ global $wp_query; if( is_archive() && $wp_query->post_count == 1 ){ the_post(); $post_url = get_permalink(); wp_redirect( $post_url ); } } add_action('template_redirect', 'redirect_to_post');<\/pre>\n\n<\/div>\n\n\n<\/pre>\n\n\n\n\nSave changes<\/li>\n<\/ol>\n\n\n\nAfter you\u2019re done with the code and you have saved the changes, you can test the feature by opening a category or tag page which contains only one post\/page in it. Instead of opening a page where only that one post\/page is shown, you will be redirected directly to the post<\/strong>. Awesome, isn\u2019t it?<\/p>\n\n\n\nSince some users might get confused by this, it would be a nice idea to include a custom message which will tell users that there was only one result found and that the redirection occurred. If you want to do that, you can leave the message on top of the post\/page a user was redirected to by inserting a simple \u201cecho\u201d statement. In that case, your code would look like this:<\/p>\n\n\n
\n \n\n
function redirect_to_post(){\nglobal $wp_query;\nif( is_archive() && $wp_query->post_count == 1 ){\nthe_post();\n$post_url = get_permalink();\nwp_redirect( $post_url );\n}\necho \"Since there was only one result found, \nyou have been redirected to this post\/page!\";\n}\nadd_action('template_redirect', 'redirect_to_post');<\/pre>\n\n<\/div>\n\n\n<\/pre>\n\n\n\nNow that even a message has been implemented, you\u2019re done and you won\u2019t have to take care of those categories and tags which contain only one post\/page. But don\u2019t let this feature be an excuse for not writing new articles in the category.<\/p>\n\n\n\n
Redirect users to a random page in WordPress<\/h2>\n\n\n\n This might not be a standard feature of every WordPress website, but sure can be a fun tool to have somewhere on your site. If you write a lot or maybe have numerous galleries, you can end up having thousands of pages which an average user won\u2019t get to see<\/strong>. So why wouldn\u2019t you make things a bit more interesting with redirecting a user to a random post?<\/p>\n\n\n\nOK, you won\u2019t be redirecting a user to a random post without their knowledge or do that on the homepage, of course. But you can create a simple button or link which a user can click after which he will be taken to another page of your site. Maybe it\u2019s a gallery. Maybe a cool editorial you had written years ago. Who knows. It can be fun.<\/p>\n\n\n\n
Let\u2019s get started:<\/p>\n\n\n\n
\nFirst you need to prepare your redirect link. Open your functions.php file<\/li>\n\n\n\n Copy and paste the following code:<\/li>\n<\/ol>\n\n\n\n \n\n
add_action('init','random_add_rewrite'); function random_add_rewrite() { global $wp; $wp->add_query_var('random'); add_rewrite_rule('random\/?<\/pre>\n\n<\/div>\n\n\n<\/pre>\n\n\n\n\nSave changes<\/li>\n\n\n\n Go to www.yourdomain.com\/?random=1<\/li>\n<\/ol>\n\n\n\n \n\n
, ‘index.php?random=1’, ‘top’); } add_action(‘template_redirect’,’random_template’); function random_template() { if (get_query_var(‘random’) == 1) { $posts = get_posts(‘post_type=post&orderby=rand&numberposts=1’); foreach($posts as $post) { $link = get_permalink($post); } wp_redirect($link,307); exit; } }<\/p>\n\n<\/div>\n\n\n
After this, you are ready to create your link\/button. All you have to do is link to http:\/\/www.yourdomain.com\/?random=1<\/em> and every time a user opens the page, he will be redirected to a random page. Enjoy your new feature.<\/p>\n\n\n\nRedirecting you to the conclusion<\/h2>\n\n\n\n Sooner or later, you will want to redirect specific users in specific situations. Whether there’s a good reason for that and you’re trying to make your visitors buy something or you just wanted to make the login process more comfortable, this tutorial will help you manage redirections.<\/p>\n\n\n\n
If you are comfortable enough with coding, you will be able to improve on this and redirect users in different situations as well. If not, we hope these examples were just enough to cover your needs.<\/p>","protected":false,"gt_translate_keys":[{"key":"rendered","format":"html"}]},"excerpt":{"rendered":"
Redirections are a standard part of websites, plugins, and themes. Some are happening right in front of eyes, and you might not be even aware what’s going on in the back. Although redirections are usually left for developers, we are going to show you several tips that will let you do that task on your […]<\/p>\n","protected":false,"gt_translate_keys":[{"key":"rendered","format":"html"}]},"author":4,"featured_media":1105,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"inline_featured_image":false,"footnotes":""},"categories":[7],"tags":[],"acf":[],"yoast_head":"\n
Redirect WordPress Users in Different Situations<\/title>\n \n \n \n \n \n \n \n \n \n \n \n \n \n\t \n\t \n\t \n \n \n \n \n \n\t \n\t \n\t \n