Skip to content

Instantly share code, notes, and snippets.

@jamesmorrison
Created August 10, 2017 18:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jamesmorrison/8018ecabbf19c132886c5d7a7d8ee242 to your computer and use it in GitHub Desktop.
Save jamesmorrison/8018ecabbf19c132886c5d7a7d8ee242 to your computer and use it in GitHub Desktop.
Rewrite search URL's to "pretty" permalinks. A custom permalink structure must be set.
<?php
/**
*
* Plugin Name: Rewrite Search URL's
* Description: Rewrite search URL's to "pretty" permalinks. A custom permalink structure must be set.
* Version: 1.0.1
* Author: James Morrison
* Author URI: https://www.jamesmorrison.me/
*
**/
add_action( 'template_redirect', function() {
// Ensure a permalink structure is set and this is a search results page request
if ( ! empty( get_option( 'permalink_structure' ) && ! empty( $_GET[ 's' ] ) ) ) {
wp_redirect( esc_attr( home_url( '/search/' . urlencode( get_query_var( 's' ) ) ) ), 302 );
exit();
}
}, 10, 0 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment