I want to add a dynamic page for every page that has URL subpage of "/team".
How would I configure "add_rewrite_rule" regex expression to allow me that?
Example:
path: http://page.com/business/team
rewrite: http://page.com/index.php?department-team=business
path: http://page.com/hr/team
rewrite: http://page.com/index.php?department-team=hr
Code I'm working with:
add_action('query_vars', 'department_team_add_query_vars');
add_action('init', 'department_team_add_rewrite_rules');
add_filter('template_include', 'department_team_template_include');
function department_team_add_query_vars($vars)
{
$vars[] = 'department-team';
return $vars;
}
function department_team_add_rewrite_rules()
{
// do I need this?
//add_rewrite_tag('%department-team%', '([^&]+)');
// rewrite url to
add_rewrite_rule(
'/([^/]*)/team',
'index.php?department-team=$matches[1]',
'top'
);
}
function department_team_template_include($template)
{
global $wp_query;
$new_template = '';
var_dump(array_key_exists('department-team', $wp_query->query_vars));
var_dump($wp_query->query_vars);
if (array_key_exists('department-team', $wp_query->query_vars)) {
switch ($wp_query->query_vars['department-team']) {
case 'team':
$new_template = locate_template(['department_team.php']);
break;
}
if ($new_template != '') {
var_dump('aaa');
die();
return $new_template;
}
else {
$wp_query->set_404();
status_header(404);
return get_404_template();
}
}
return $template;
}