0

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;
}

2 Answers 2

1

I do something similar. I have a page (country_tc) that uses a custom page template that either dynamically generates info for requested country, or a series of sub-pages for the country e.g.

/country/Egypt (index.php?pagename=country&countrytc=egypt)

/country/egypt/safety (index.php?pagename=country&countrytc=egypt/safety)

I use page_rewrite_rules for the re-write. I've not had time to vet your code, so this is how I'd do it:

In a site functions plugin:

//  allow WP to store querystring attribs for use in our pages
function tc_query_vars_filter($vars) {
  $vars[] = 'department-team';
  $vars[] .= 'another-var';
  return $vars;
}
add_filter( 'query_vars', 'tc_query_vars_filter' );

function tc_rewrite_rules($rules) {
   global $wp_rewrite;
   $tc_rule = array(
     // working example from my site
     'country/(.+)/?' => 'index.php?pagename=' . 'country' . '&countrytc=$matches[1]',
     // YOUR rule (not tested)
     '/([^/]*)/team', 'index.php?pagename=YOURPAGENAME&department-team=$matches[1]'
   );
   return array_merge($tc_rule, $rules);
}
add_filter('page_rewrite_rules', 'tc_rewrite_rules');

AFTER ACTIVATING PLUGIN YOU NEED TO RE-SAVE PERMALINKS ON DASHBOARD - THIS WILL FLUSH/UPDATE YOUR REWRITE RULES

Do the rest of your code in your custom page using:

get_query_var('department-team') - sanitize, validate do your includes as required.

0

Use add_rewrite_endpoint instead, it generates the rules for you.

add_rewrite_endpoint( 'team', EP_PAGES );
2
  • Not sure if I'm implkementing this correctly as department-team is still not located in $wp_query->query_vars.
    – HelpNeeder
    Commented Sep 25, 2017 at 17:19
  • it would be team in this case, the query var has the same name as the endpoint.
    – Milo
    Commented Sep 25, 2017 at 22:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.