How do I redirect /search/ to wordpress search template?

You can use template_redirect. Here a simple redirection function. add_action( ‘template_redirect’, ‘se219663_template_redirect’ ); function se219663_template_redirect() { global $wp_rewrite; if ( is_search() && ! empty ( $_GET[‘s’] ) ) { $s = sanitize_text_field( $_GET[‘s’] ); // or get_query_var( ‘s’ ) $location = “https://wordpress.stackexchange.com/”; $location .= trailingslashit( $wp_rewrite->search_base ); $location .= user_trailingslashit( urlencode( $s ) ); $location … Read more

template_include (overriding default plugin templates via current theme)

So the template_redirect is used for things such as canonicalisation, feeds etc. If you want to alter the template that is served template_include is preferred. Unlike template_redirect, template_include is a filter which filters the path of the template page. This means you don’t load/include the template, but just return the template path. WordPress does a … Read more

Custom Post Types and template_redirect

You should be using the template_include filter for this: add_filter(‘template_include’, ‘wpse_44239_template_include’, 1, 1); function wpse_44239_template_include($template){ global $wp_query; //Do your processing here and define $template as the full path to your alt template. return $template; } template_redirect is the action called directly before headers are sent for the output of the rendered template. It’s a convenient … Read more

single-{$post_type}-{slug}.php for custom post types

A) The Base in Core As you can see in the Codex Template Hierarchy explanation, single-{$post_type}.php is already supported. B) Extending the core Hierarchy Now there’re gladly some filters and hooks inside /wp-includes/template-loader.php. do_action(‘template_redirect’); apply_filters( ‘template_include’, $template ) AND: a specific filter inside get_query_template( $type, … ) named: “$type}_template” B.1) How it works Inside the … Read more

How to create custom URL routes?

Add this to your theme’s functions.php, or put it in a plugin. add_action( ‘init’, ‘wpse26388_rewrites_init’ ); function wpse26388_rewrites_init(){ add_rewrite_rule( ‘properties/([0-9]+)/?$’, ‘index.php?pagename=properties&property_id=$matches[1]’, ‘top’ ); } add_filter( ‘query_vars’, ‘wpse26388_query_vars’ ); function wpse26388_query_vars( $query_vars ){ $query_vars[] = ‘property_id’; return $query_vars; } This adds a rewrite rule which directs requests to /properties/ with any combination of numbers following to … Read more