1

I have the following rewrite tags defined:

// ADD REWRITE TAG FOR 'VEHICLE MAKES'
add_rewrite_tag('%make%','([^&]+)');

// ADD REWRITE TAG FOR 'BODY STYLES'
add_rewrite_tag('%body-style%','([^&]+)');

// ADD REWRITE TAG FOR 'OTHER TOPICS'
add_rewrite_tag('%topic%','([^&]+)');

I'm trying to create a custom rewrite for each one of these that can apply to any page the variables exist.

I've been reworking the WP example in the documentation with no luck.. This rewrite is for 'Vehicle Makes'.

I'm aiming for WP to interpret the following:

http://example.com/make/abc

as

http://example.com?make=abc

and the same pattern for

http://example.com?body-style=abc & http://example.com?topic=abc

I can't seem to get the following to work no matter how much I try

add_rewrite_rule('^nutrition/([^/]*)/([^/]*)/?','index.php?page_id=12&food=$matches[1]&variety=$matches[2]','top');

Any and All help is appreciated!

  • How did you go with this @AustinTBiggs? – Tim Malone Jun 29 '16 at 3:19
2

Have you tried adding the third parameter to that call?

add_rewrite_tag( '%make%', '([^/]+)', 'make=' );

0

Before asking any question here, please try to find it out yourself. In case of wordpress you should look at http://codex.wordpress.org/ first.

Most of the answers are available already.

The piece of code you are looking for is a function flush_rewrite_rules().

Ref: Rewrite Rules

  • That is incorrect, I did a lot of searching for the correct answer and it's not simply flushing the rewrite rules. I always flush rules after they've been modified. – Austin Biggs Sep 3 '14 at 15:36
0

The way of using add_rewrite_rule function is given below:

  1. Hook this function into the init hook
  2. Use query_vars filter to make the new rewrite available in the query_vars

Here is the code snippet

add_action('init', 'my_custom_rewrite_rule');
function my_custom_rewrite_rule() {
    add_rewrite_rule( 'make/([^/]+)/?$', 'index.php?make=$matches[1]', 'top' );
}

add_filter('query_vars', 'my_rewrite_query_var');
function my_rewrite_query_var( $vars ) {
    $vars[] = 'make';
    return $vars;
}

Now, go to permalink settings page, and click on Save Changes to flush the rewrite rules.

  • Comes out blank when you try and do a get_query_var('make') – adamj Aug 24 '17 at 11:02
  • Did you flush rewrite rules? – Saikat Aug 25 '17 at 11:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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