Repository navigation
Query: Redirect searches for random content to the search results. - #15916
aaronjorbin wants to merge 2 commits into
Conversation
A search request that includes the `random` query variable redirected to a matching post or returned a 404, which revealed whether any post matched the search terms. Search requests are no longer random content requests. Instead they are redirected to the same search without the `random` query variable or the random base, before the main query is run. The redirect retains the requested path and the other query string parameters, URL-encodes parameter names as well as values, and is skipped when the location would be the requested URL. Requests for robots.txt, the favicon, sitemaps, feeds and trackbacks now ignore the `random` query variable, they are neither random content requests nor redirected. See #66662. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. Core Committers: Use this line as a base for the props when committing in SVN: To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. Props Bot commandsYou can trigger Props Bot actions by commenting on this PR:
|
There was a problem hiding this comment.
🟡 Changes recommended
Redirect URL construction mishandles backslashes in request paths and parameter names, potentially changing search terms or collapsing distinct query parameters.
2 open issues
What changed in this PR
This PR separates search requests from WordPress random-content selection, redirecting searches to ordinary results before the main query runs.
Changes:
- Adds early search redirects that remove random-content routing while retaining other request parameters.
- Excludes searches and non-post endpoints from random-content handling.
- Updates documentation and expands regression coverage for redirects, encoding, and request exclusions.
| File | Description |
|---|---|
tests/phpunit/tests/query/randomContentRedirect.php |
Replaces random-search expectations and adds redirect regression tests. |
src/wp-includes/query.php |
Updates documentation for random-content eligibility. |
src/wp-includes/class-wp.php |
Adds early search redirects and query-parameter encoding. |
src/wp-includes/class-wp-rewrite.php |
Documents search redirects instead of random-search selection. |
src/wp-includes/class-wp-query.php |
Excludes searches and non-post endpoints from random-query classification. |
Review effort: Balanced
| $requested_path = (string) preg_replace_callback( | ||
| '|[^a-z0-9\-._~+%/]|i', | ||
| static fn ( array $matches ): string => rawurlencode( $matches[0] ), | ||
| $this->request |
| $encoded = array(); | ||
|
|
||
| foreach ( $query_args as $name => $value ) { | ||
| $name = urlencode( wp_unslash( (string) $name ) ); |
Test using WordPress PlaygroundThe changes in this pull request can previewed and tested using a WordPress Playground instance. WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser. Some things to be aware of
For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation. |

A search request that includes the
randomquery variable redirected to a matching post or returned a 404, which revealed whether any post matched the search terms. Search requests are no longer random content requests. Instead they are redirected to the same search without therandomquery variable or the random base, before the main query is run.The redirect retains the requested path and the other query string parameters, URL-encodes parameter names as well as values, and is skipped when the location would be the requested URL.
Requests for robots.txt, the favicon, sitemaps, feeds and trackbacks now ignore the
randomquery variable, they are neither random content requests nor redirected.Trac Ticket: https://core.trac.wordpress.org/ticket/66662
Use of AI Tools
Example disclosure:
AI assistance: Yes
Tool(s): Claude
Model(s): Fabel 5.2
Used for: Code Generation, Code Review, Risk Analysis
This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.