WordPress.org

Make WordPress Core

Opened 7 years ago

Closed 2 years ago

Last modified 2 years ago

#16840 closed enhancement (fixed)

add_rewrite_rule() should accept an array for the $redirect parameter

Reported by: scribu Owned by: wonderboymusic
Milestone: 4.4 Priority: normal
Severity: normal Version:
Component: Rewrite Rules Keywords: has-patch needs-testing has-unit-tests
Focuses: Cc:

Description

The most common usage of add_rewrite_rule() looks something like this:

add_rewrite_rule( 'some/path/([^/]+)/?$', 'index.php?some_var=$matches[1]&maybe_some_flag=1' );

That's not very pretty.

Here's what it could look like instead:

add_rewrite_rule( 'some/path/([^/]+)/?$', array(
  'some_var' => '$matches[1]', 
  'maybe_some_flag' => 1
) );

In the rare case when you want to use something other than 'index.php' in the front (i.e. an external rule), you can either fall back to the old form or just use add_external_rule().

Attachments (4)

16840.diff (1.7 KB) - added by scribu 7 years ago.
test-16840.php (637 bytes) - added by scribu 7 years ago.
16840.2.diff (6.2 KB) - added by DrewAPicture 2 years ago.
16840.3.diff (6.5 KB) - added by DrewAPicture 2 years ago.

Download all attachments as: .zip

Change History (15)

@scribu
7 years ago

@scribu
7 years ago

#1 @mikeschinkel
7 years ago

  • Cc mikeschinkel@… added

#2 @MZAWeb
6 years ago

  • Cc MZAWeb added

#3 @stephenh1988
6 years ago

  • Cc stephen@… added

#4 @Mamaduka
6 years ago

  • Cc georgemamadashvili@… added

#5 @chriscct7
2 years ago

  • Keywords needs-refresh added

#6 @johnbillion
2 years ago

  • Keywords needs-unit-tests added
  • Milestone changed from Awaiting Review to 4.4

#7 @DrewAPicture
2 years ago

  • Keywords needs-testing has-unit-tests added; needs-refresh needs-unit-tests removed
  • Owner set to DrewAPicture
  • Status changed from new to accepted

16840.2.diff updates the patch including docs and adds tests both for the add_rewrite_rule() wrapper and WP_Rewrite::add_rule().

@DrewAPicture
2 years ago

#8 @DrewAPicture
2 years ago

  • Owner changed from DrewAPicture to wonderboymusic
  • Status changed from accepted to assigned

@DrewAPicture
2 years ago

#9 @DrewAPicture
2 years ago

16840.3.diff adds stricter checks in the tests after feedback from @boonebgorges.

#10 @wonderboymusic
2 years ago

  • Resolution set to fixed
  • Status changed from assigned to closed

In 34708:

Rewrite: allow add_rewrite_rule|WP_Rewrite::add_rule() to accept an associative array for the value of $redirect instead of requiring a query string.

Adds unit tests.

Props scribu, DrewAPicture.
Fixes #16840.

#11 @wonderboymusic
2 years ago

In 34847:

Rewrite: add tests for add_rewrite_rule().

Props DrewAPicture.
See #16840.

Note: See TracTickets for help on using tickets.