# How can I insert a template into base.php when using a custom rewrite rule?

**URL:** https://discourse.roots.io/t/how-can-i-insert-a-template-into-base-php-when-using-a-custom-rewrite-rule/10833
**Category:** sage
**Tags:** sage8
**Created:** 2017-11-06T08:09:21Z
**Posts:** 4

## Post 1 by @Paul_Benbow — 2017-11-06T08:09:21Z

I am creating a custom rewrite rule so I can implement a postcode search of a CPT. I have manged to insert a custom template but it outputs in the Head currently. I tried including a new base-custom.php but it just loads base.php. Do I need to filter SageWrapping somehow?

```
class Experts {

	public function __construct() {
		add_action( 'init', array( $this, 'expert_routes' ) );
		add_filter( 'query_vars', array( $this, 'manage_expert_routes_query_vars') );
		add_action( 'template_redirect', array( $this, 'front_controller' ) );
		add_action( 'before_expert_postcode_search', array( $this, 'expert_postcode_search_results' ) );
	}

	public function expert_routes() {
		add_rewrite_rule( '^expert/([^/]+)/?', 'index.php?control_action=$matches[1]', 'top' );
	}

	public function manage_expert_routes_query_vars( $query_vars ) {
		$query_vars[] = 'control_action';
		return $query_vars;
	}

	public function front_controller() {

		global $wp_query;
		$control_action = isset ( $wp_query->query_vars['control_action'] ) ? $wp_query->query_vars['control_action'] : '';
		
		switch ( $control_action ) {
			case 'search':
				do_action( 'before_expert_postcode_search' );
			break;
		}
	}

	public function expert_postcode_search_results(){
		get_template_part('templates/content-experts-search'); // Loads in HEAD
		//include('base-template-expert-search.php'); // Just loads base.php
	}

}
```

---

## Post 2 by @MWDelaney — 2017-11-06T14:02:06Z

From context cues I’m guessing you’re using Sage 8. If you were in Sage 9 I would recommend a `@stack` (see details [here](https://discourse.roots.io/t/how-blade-stacks-make-my-life-better/10121)). With 8 you could probably get away with a conditional and [get\_post\_type](https://developer.wordpress.org/reference/functions/get_post_type/).

---

## Post 3 by @Paul_Benbow — 2017-11-07T14:10:44Z

@MWDelaney I’m assuming you mean a conditional check in base.php around

`include Wrapper\template_path();`

That didn’t sound quite what I was after but I have come up with the following options.

1. Change the Template before Sage hook into this.

`add_filter('template_include', [__NAMESPACE__. '\\SageWrapping', 'wrap'], 109);`

```
add_filter( 'template_include', array( $this, 'sz_change_inserted_template' ), 99 );
public function sz_change_inserted_template( $template ) {
	return get_template_directory() . '\templates\content-experts-search.php';
}
```

1. Use the filer in SageWrapper to change the base wrapper.

`add_filter('sage/wrap_base', array( $this, 'sz_swap_base_template'));`

```
public function sz_swap_base_template($templates) {
	array_unshift($templates, 'base-template-expert-search.php'); // Shift the template to the front of the array
	return $templates; // Return our modified array with base-$cpt.php at the front of the queue
}
```

---

## Post 4 by @MWDelaney — 2017-11-07T14:24:32Z

I actually just meant doing this in `base.php`:

```
if('the-post-type' == get_post_type()) {
  get_template_part('templates/content-experts-search');
}
```
