Title: Template loading proplem
Last modified: August 31, 2016

---

# Template loading proplem

 *  [juliston](https://wordpress.org/support/users/juliston/)
 * (@juliston)
 * [10 years, 4 months ago](https://wordpress.org/support/topic/template-loading-proplem/)
 * I found that problem you have used the hook template_redirect for loading the
   better search template. The problem with this code is that anything hooked in
   to template_redirect after this code isn’t going to run! Could you please use
   the hook template_include instead of template_redirect hook.
    So we change the
   code in the file
 * better-search/includes/template-redirect.php
 * we change the function
 * function bsearch_template_redirect() {
    // not a search page; don’t do anything
   and return if ( ( stripos( $_SERVER[‘REQUEST_URI’], ‘?s=’ ) === false ) && ( 
   stripos( $_SERVER[‘REQUEST_URI’], ‘/search/’ ) === false ) && ( ! is_search())){
   return; }
 *  global $wp_query, $bsearch_settings;
 *  // If seamless integration mode is activated; return
    if ( $bsearch_settings[‘
   seamless’] ) { return; }
 *  // if we have a 404 status
    if ( $wp_query->is_404 ) { // set status of 404 
   to false $wp_query->is_404 = false; $wp_query->is_archive = true; }
 *  // change status code to 200 OK since /search/ returns status code 404
    [@header](https://wordpress.org/support/users/header/)(‘
   HTTP/1.1 200 OK’, 1 ); [@header](https://wordpress.org/support/users/header/)(‘
   Status: 200 OK’, 1 );
 *  $search_query = get_bsearch_query();
 *  $limit = isset( $_GET[‘limit’] ) ? intval( $_GET[‘limit’] ) : $bsearch_settings[‘
   limit’]; // Read from GET variable
 *  // Added necessary code to the head
    add_action( ‘wp_head’, ‘bsearch_head’ );
 *  // Set thw title
    add_filter( ‘wp_title’, ‘bsearch_title’ );
 *  // If there is a template file within the parent or child theme then we use 
   it
    $priority_template_lookup = array( get_stylesheet_directory() . ‘/better-
   search-template.php’, get_template_directory() . ‘/better-search-template.php’,
   plugin_dir_path( dirname( __FILE__ ) ) . ‘templates/template.php’, );
 *  foreach ( $priority_template_lookup as $exists ) {
 *  if ( file_exists( $exists ) ) {
 *  include_once( $exists );
    exit;
 *  }
    } } add_action( ‘template_redirect’, ‘bsearch_template_redirect’, 1 );
 * to
 * function bsearch_template_redirect($template) {
    // not a search page; don’t 
   do anything and return if ( ( stripos( $_SERVER[‘REQUEST_URI’], ‘?s=’ ) === false)&&(
   stripos( $_SERVER[‘REQUEST_URI’], ‘/search/’ ) === false ) && ( ! is_search())){
   return $template; }
 *  global $wp_query, $bsearch_settings;
 *  // If seamless integration mode is activated; return
    if ( $bsearch_settings[‘
   seamless’] ) { return $template; }
 *  // if we have a 404 status
    if ( $wp_query->is_404 ) { // set status of 404 
   to false $wp_query->is_404 = false; $wp_query->is_archive = true; }
 *  // change status code to 200 OK since /search/ returns status code 404
    [@header](https://wordpress.org/support/users/header/)(‘
   HTTP/1.1 200 OK’, 1 ); [@header](https://wordpress.org/support/users/header/)(‘
   Status: 200 OK’, 1 );
 *  $search_query = get_bsearch_query();
 *  $limit = isset( $_GET[‘limit’] ) ? intval( $_GET[‘limit’] ) : $bsearch_settings[‘
   limit’]; // Read from GET variable
 *  // Added necessary code to the head
    add_action( ‘wp_head’, ‘bsearch_head’ );
 *  // Set thw title
    add_filter( ‘wp_title’, ‘bsearch_title’ );
 *  // If there is a template file within the parent or child theme then we use 
   it
    $priority_template_lookup = array( get_stylesheet_directory() . ‘/better-
   search-template.php’, get_template_directory() . ‘/better-search-template.php’,
   plugin_dir_path( dirname( __FILE__ ) ) . ‘templates/template.php’, );
 *  foreach ( $priority_template_lookup as $exists ) {
 *  if ( file_exists( $exists ) ) {
 *  return $exists;
    //include_once( $exists ); //exit;
 *  }
    }
 *  return $template;
    } add_action( ‘template_include’, ‘bsearch_template_redirect’,
   1 );
 * [https://wordpress.org/plugins/better-search/](https://wordpress.org/plugins/better-search/)

Viewing 3 replies - 1 through 3 (of 3 total)

 *  Plugin Author [WebberZone](https://wordpress.org/support/users/webberzone/)
 * (@webberzone)
 * [10 years, 4 months ago](https://wordpress.org/support/topic/template-loading-proplem/#post-7033135)
 * Hi, the code above got completely mixed up in formatting.
 * Do you mind re-posting it enclosed within the code button in the forum, using
   a gist?
 *  Thread Starter [juliston](https://wordpress.org/support/users/juliston/)
 * (@juliston)
 * [10 years, 3 months ago](https://wordpress.org/support/topic/template-loading-proplem/#post-7033254)
 *     ```
       function bsearch_template_redirect($template) {
       	// not a search page; don't do anything and return
       	if ( ( stripos( $_SERVER['REQUEST_URI'], '?s=' ) === false ) && ( stripos( $_SERVER['REQUEST_URI'], '/search/' ) === false ) && ( ! is_search() ) ) {
       		return $template;
       	}
   
       	global $wp_query, $bsearch_settings;
   
       	// If seamless integration mode is activated; return
       	if ( $bsearch_settings['seamless'] ) {
       	    return $template;
       	}
   
       	// if we have a 404 status
       	if ( $wp_query->is_404 ) {
       		// set status of 404 to false
       		$wp_query->is_404 = false;
       		$wp_query->is_archive = true;
       	}
   
       		// change status code to 200 OK since /search/ returns status code 404
       	@header( 'HTTP/1.1 200 OK', 1 );
       	@header( 'Status: 200 OK', 1 );
   
       	$search_query = get_bsearch_query();
   
       	$limit = isset( $_GET['limit'] ) ? intval( $_GET['limit'] ) : $bsearch_settings['limit']; // Read from GET variable
   
       		// Added necessary code to the head
       	add_action( 'wp_head', 'bsearch_head' );
   
       		// Set thw title
       	add_filter( 'wp_title', 'bsearch_title' );
   
       	// If there is a template file within the parent or child theme then we use it
       	$priority_template_lookup = array(
       		get_stylesheet_directory() . '/better-search-template.php',
       		get_template_directory() . '/better-search-template.php',
       		plugin_dir_path( dirname( __FILE__ ) ) . 'templates/template.php',
       	);
   
       	foreach ( $priority_template_lookup as $exists ) {
   
       		if ( file_exists( $exists ) ) {
   
       			return $exists;
       			//include_once( $exists );
       			//exit;
   
       		}
       	}
   
       	return $template;
       }
       add_action( 'template_include', 'bsearch_template_redirect', 1 );
       ```
   
 *  Plugin Author [WebberZone](https://wordpress.org/support/users/webberzone/)
 * (@webberzone)
 * [10 years, 3 months ago](https://wordpress.org/support/topic/template-loading-proplem/#post-7033274)
 * Thanks. I’ll take a look at this and run a few tests before implementation in
   the final version

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Template loading proplem’ is closed to new replies.

 * ![](https://ps.w.org/better-search/assets/icon-256x256.png?rev=2988729)
 * [Better Search - Relevant search results for WordPress](https://wordpress.org/plugins/better-search/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/better-search/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/better-search/)
 * [Active Topics](https://wordpress.org/support/plugin/better-search/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/better-search/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/better-search/reviews/)

 * 3 replies
 * 2 participants
 * Last reply from: [WebberZone](https://wordpress.org/support/users/webberzone/)
 * Last activity: [10 years, 3 months ago](https://wordpress.org/support/topic/template-loading-proplem/#post-7033274)
 * Status: not resolved