[wp-hackers] Enabling pretty url search redirect

Tom Barrett tcbarrett at gmail.com
Wed Nov 21 16:07:21 UTC 2012


I want to redirect all front end searches (?s=) onto the /search/ pretty
permalink. For pages (?p=) this happens automatically when permalinks are
switched on, but search is different.

If that isn't clear, here are two links. The first redirects to the pretty
url, the second doesn't:
http://www.tcbarrett.com/?p=1038
http://www.tcbarrett.com/?s=donate

I'm currently using a hook:
add_action( 'template_redirect', 'tcb_search_pretty_permalinks' );
function tcb_search_pretty_permalinks(){
  if( !is_admin() and is_search() ):
    if( $search = $_GET['s'] ):
      wp_redirect( home_url("search/{$search}/") );
      exit;
    endif;
  endif;
}

But wondered if there was a better way? I'm a little worried that I've
missed something and am now breaking things...

-- 
http://www.tcbarrett.com | http://gplus.to/tcbarrett |
http://twitter.com/tcbarrett


More information about the wp-hackers mailing list