Making WordPress.org

Changeset 750


Ignore:
Timestamp:
07/17/2014 07:11:17 PM (10 years ago)
Author:
coffee2code
Message:

Code Reference: redirect a search query with only one result directly to that result. Fixes #553

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/themes/pub/wporg-developer/functions.php

    r746 r750  
    4848    add_action( 'widgets_init', __NAMESPACE__ . '\\widgets_init' );
    4949    add_action( 'pre_get_posts', __NAMESPACE__ . '\\pre_get_posts' );
     50    add_action( 'template_redirect', __NAMESPACE__ . '\\redirect_single_search_match' );
    5051    add_action( 'wp_enqueue_scripts', __NAMESPACE__ . '\\theme_scripts_styles' );
    5152    add_filter( 'post_type_link', __NAMESPACE__ . '\\method_permalink', 10, 2 );
     
    434435    return $excerpt;
    435436}
     437
     438/**
     439 * Redirects a search query with only one result directly to that result.
     440 */
     441function redirect_single_search_match() {
     442    if ( is_search() && 1 == $GLOBALS['wp_query']->found_posts ) {
     443        wp_redirect( get_permalink( get_post() ) );
     444        exit();
     445    }
     446}
     447
Note: See TracChangeset for help on using the changeset viewer.