Change the WordPress Search URL Query String

By default WordPress uses the "s" parameter as the query string to perform a site search. In some cases you may want to change this parameter to a different string. You can do this easily with the following function:

function change_search_url_rewrite() {
    if ( is_search() && ! empty( $_GET['s'] ) ) {
        wp_redirect( home_url( "/search/" ) . urlencode( get_query_var( 's' ) ) );
        exit();
    }
}
add_action( 'template_redirect', 'change_search_url_rewrite' );

After adding this function, the search URL will be search/test instead of ?s=test

Discussion
[  0 Comments  ]

Leave a Comment

To add code, use the buttons below. For instance, click the PHP button to insert PHP code within the shortcode. If you notice any typos, please let us know!