• 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( ‘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 ) ) {

    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( ‘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 );

    https://ww.wp.xz.cn/plugins/better-search/

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author WebberZone

    (@webberzone)

    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

    (@juliston)

    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

    (@webberzone)

    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.