Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
d434e33
Tests for additional random rewrites.
peterwilsoncc Nov 9, 2026
2f3742d
Add test for rewrite rule defined as `random`.
peterwilsoncc Nov 9, 2026
34f6fb7
Use existing rewrite rules to test if /random is taken.
peterwilsoncc Nov 9, 2026
bb09f68
Tidy up new filter: typos, docs and passing the slug alone.
peterwilsoncc Nov 9, 2026
d170d3b
Remove duplicate test.
peterwilsoncc Nov 9, 2026
d88426e
Move `test_single_post_of_a_post_type_at_the_random_base_is_not_redir…
peterwilsoncc Nov 9, 2026
9185b45
Rename data provider for use with a single test.
peterwilsoncc Nov 9, 2026
4b64321
Merge mergable tests.
peterwilsoncc Nov 9, 2026
b6a012b
Document preceident of rewrite rules.
peterwilsoncc Nov 9, 2026
a4c7f9b
Rename single use data providers for their test.
peterwilsoncc Nov 9, 2026
23001cd
Revert "Rename single use data providers for their test."
peterwilsoncc Nov 9, 2026
fa74dad
Merge branch 'trunk' into test/random-post-type-priority
peterwilsoncc Nov 9, 2026
85cb495
Expanded test description.
peterwilsoncc Nov 10, 2026
c9d6608
Fix docblock type.
peterwilsoncc Nov 10, 2026
a4cb489
Merge branch 'trunk' into test/random-post-type-priority
peterwilsoncc Nov 10, 2026
b692b4b
Is @aaronjorbin trolling?
peterwilsoncc Nov 10, 2026
28be967
Exclude bottom rules.
peterwilsoncc Nov 10, 2026
f10e2da
KISS.
peterwilsoncc Nov 10, 2026
fe56057
Remove duplicate filter test.
peterwilsoncc Nov 10, 2026
9433be4
Merge branch 'trunk' into test/random-post-type-priority
peterwilsoncc Nov 10, 2026
d72e709
Remove new filter in favour of existing `random_rewrite_rules`.
peterwilsoncc Nov 10, 2026
d23af89
Revert source code changes.
peterwilsoncc Nov 10, 2026
8916b12
Revert "Revert source code changes."
peterwilsoncc Nov 10, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 45 additions & 17 deletions src/wp-includes/class-wp-rewrite.php
Original file line number Diff line number Diff line change
Expand Up @@ -1423,22 +1423,6 @@ public function rewrite_rules() {
*/
$page_rewrite = apply_filters( 'page_rewrite_rules', $page_rewrite );

// Random content rewrite rules.
$random_rewrite = $this->random_rewrite_rules();

/**
* Filters rewrite rules used for random content redirects.
*
* Likely random content rules would include `/random/`, as well as
* term, author and post type archives prefixed with the random base,
* for example `/random/category/news/`.
*
* @since 7.2.0
*
* @param array<string, string> $random_rewrite Array of rewrite rules for random content redirects, keyed by their regex pattern.
*/
$random_rewrite = apply_filters( 'random_rewrite_rules', $random_rewrite );

// Extra permastructs.
foreach ( $this->extra_permastructs as $permastructname => $struct ) {
if ( is_array( $struct ) ) {
Expand Down Expand Up @@ -1485,7 +1469,38 @@ public function rewrite_rules() {
$this->extra_rules_top = array_merge( $this->extra_rules_top, $rules );
}

// Put them together.
// Random content rewrite rules.
$random_rewrite = $this->random_rewrite_rules();

/*
* Rules added to the top, including those for post types and taxonomies, take
* precedence over random content rules using the same regex, for example a post
* type archive at `/random/`.
*
* Rules added to the top are merged ahead of the random content rules, so without
* this, the random content rule's query would replace the query of a rule using the
* same regex. Rules added to the top using a different regex for the same path,
* such as `^random/?$`, already take precedence as they are matched first.
*/
$random_rewrite = array_diff_key( $random_rewrite, $this->extra_rules_top );

/**
* Filters rewrite rules used for random content redirects.
*
* Likely random content rules would include `/random/`, as well as
* term, author and post type archives prefixed with the random base,
* for example `/random/category/news/`.
*
* @since 7.2.0
*
* @param array<string, string> $random_rewrite Array of rewrite rules for random content redirects, keyed by their regex pattern.
*/
$random_rewrite = apply_filters( 'random_rewrite_rules', $random_rewrite );

/*
* Put them together, with the random content rules ahead of the generic rules
* such as the page and post rules which would otherwise match them first.
*/
if ( $this->use_verbose_page_rules ) {
$this->rules = array_merge( $this->extra_rules_top, $robots_rewrite, $favicon_rewrite, $sitemap_rewrite, $deprecated_files, $registration_pages, $random_rewrite, $root_rewrite, $comments_rewrite, $search_rewrite, $author_rewrite, $date_rewrite, $page_rewrite, $post_rewrite, $this->extra_rules );
} else {
Expand Down Expand Up @@ -1532,6 +1547,19 @@ public function rewrite_rules() {
* the URL, see {@see WP_Rewrite::post_permalinks_share_random_base()}. The rules are flushed
* when such a page or post changes, see {@see wp_random_content_flush_rewrite_rules_for_post()}.
*
* Rules added to the top of the rewrite rules, including those for post types and taxonomies,
* take precedence over the random content rules. When the rewrite rules are generated, random
* content rules using the same regex as a rule added to the top are omitted, see
* {@see WP_Rewrite::rewrite_rules()}. For example, a post type archive at `/random/` replaces
* the `/random/` rule.
*
* If a post type or taxonomy uses the random base as its rewrite slug, its rules may match
* random archive URLs before the random content rules do. For example, the attachment rules
* of a post type with the rewrite slug `random` match `/random/category/news/`, and the rules
* of a hierarchical post type or taxonomy match every URL beginning with `/random/`. Such
* random archive URLs display the post type or taxonomy content instead. To use random
* archives on these sites, change the random base on the Permalinks screen.
*
* The `random` query variable can be added to any other archive or search URL, for example
* `/2026/?random`, to request a random post from that archive.
*
Expand Down
Loading
Loading