Best Wordpress themes and plugins

How to Auto-Redirect When WordPress Search Query Only Returns One Match


WordpressComp -
How to Auto-Redirect When WordPress Search Query Only Returns One Match
Rate it!
Rate this post

The way to Auto-Redirect When WordPress Search Question Solely Returns One Match obtain the WordPress Theme

Obtain now How one can Auto-Redirect When WordPress Search Question Solely Returns One Match theme.
Evaluate and opinions of Tips on how to Auto-Redirect When WordPress Search Question Solely Returns One Match theme.

Typically is the little issues that make an enormous distinction. Saving your guests from a click on right here and a scroll there could make the consumer expertise just a bit extra pleasant. This will additional assist in boosting the conversion rates as properly.

In case you present your guests with the power to look your web site, there might be occasions when the outcomes returned is perhaps only a single web page or submit. In these conditions, it may be useful to direct guests to the person submit as an alternative of to a search outcomes web page the place they need to then click on by way of to a single outcome.

How-to-Auto-Redirect-When-WordPress-Search-Query-Only-Returns-One-Match

On this WordPress tutorial, we’ll present you how to set up your website to carry out an auto redirect any time a consumer search question returns just one potential match.

Find out how to Auto-Redirect When WordPress Search Question Solely Returns One Match

This can be a comparatively straightforward enchancment to make, however there are some things you should be aware of earlier than you get began. The primary challenge is that your features.php file is a important file. In the event you make an error when modifying this file, you can simply break your web site–not good. Every time potential it is best to keep away from making modifications on to your features.php file.

There are a number of potential options to managing this problem:

  1. Use a child theme and make any modifications essential to the kid theme information.
  2. Use a WordPress plugin like Code Snippets which is out there free of charge within the WordPress plugin repository.
  3. Create a plugin.

Okay, now that you’ve a greater understanding of the significance of managing your customized features correctly. Let’s check out how one can make the required modifications to your search performance. We’re going to imagine that we’re modifying a toddler theme for this instance. Listed here are the steps:

1 Login to your cPanel, and open up your file supervisor.

cPanel-File-Manager
Login to Cpanel, open File Supervisor and Navigate to /wp-content material/themes/youngster-theme-identify/ Folder.

2 Create a Backup of Features.php.

backup-functions-php-file

All the time create a backup file earlier than modifying

Inside your baby theme folder, choose the features.php file and click on on copy. When the popup opens, underneath “Copy file to:” add the next: /features-backup.php – this file will now function your backup in case one thing goes mistaken together with your unique file. If that have been the case, you possibly can easy delete your damaged file and rename the copy to only features.php.

three Edit Features.php.

edit-functions-php-file
Subsequent, choose the features.php file and click on on Edit (click on edit a second time when the popup field seems).
Scroll to the underside of your file and paste within the following code as seen within the image under:

add_action('template_redirect', 'redirect_single_post');
perform redirect_single_post() 
  if (is_search()) 
    international $wp_query;
    if ($wp_query->post_count == 1) 
      wp_redirect( get_permalink( $wp_query->posts['0']->ID ) );
      exit;
    
  

four Paste the code.

WordPress-search-redirect-functions-php

Paste the code above into the underside of the features.php file and save

Click on Save within the prime proper nook and shut the window.

When you’re accomplished that, head to your website and check out the search perform. For those who enter an obscure search time period for which there’s just one end result, you must now discover that you’re redirected to the precise publish as an alternative of a search end result. That’s it!

Easy methods to Auto-Redirect When WordPress Search Question Solely Returns One Match obtain the WordPress Theme

Obtain now Tips on how to Auto-Redirect When WordPress Search Question Solely Returns One Match theme.
Evaluation and opinions of Easy methods to Auto-Redirect When WordPress Search Question Solely Returns One Match theme.

Rate it!
Rate this post

Leave a Reply

Your email address will not be published. Required fields are marked *

How to Auto-Redirect When WordPress Search Query Only Returns One Match
Voted 4.2/5 of 6 reviews.
Categories
Recent