Repository navigation
Query: Send the random content redirect on template_redirect. - #15912
aaronjorbin wants to merge 3 commits into
Conversation
The random content redirect was sent from `WP::handle_random()` before the `template_redirect` action, so plugins restricting access to a site on that action never saw the request and logged-out visitors were redirected to post URLs. The redirect is now sent by `wp_random_content_redirect()` on `template_redirect` at priority 100. `redirect_canonical()` no longer redirects random content requests that have a post to redirect to, so these requests are not redirected twice. Trac Ticket: https://core.trac.wordpress.org/ticket/66661
|
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:
|
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. |
There was a problem hiding this comment.
🟡 Changes recommended
Random redirects can still be preempted by canonical redirects or skipped when an earlier callback replaces $wp_query, requiring fixes and regression coverage for both cases.
2 open issues
What changed in this PR
Moves random-content redirects to template_redirect so earlier access-control callbacks can run before visitors are redirected to a selected post.
Changes:
- Schedules the redirect at priority 100 when the main query selects a post.
- Updates tests for deferred execution and canonical-redirect interactions.
- Documents the new redirect timing.
| File | Description |
|---|---|
tests/phpunit/tests/query/randomContentRedirect.php |
Fires template_redirect in tests and adds timing and canonical-interaction assertions. |
src/wp-includes/query.php |
Updates redirect-helper documentation; no implementation changes. |
src/wp-includes/class-wp.php |
Defers successful random redirects while retaining immediate 404 handling for empty results. |
Review effort: Balanced
|
|
||
| if ( wp_is_random_content_query( $wp_the_query ) && ! $wp_the_query->post instanceof WP_Post ) { | ||
| if ( $wp_the_query->post instanceof WP_Post ) { | ||
| add_action( 'template_redirect', 'wp_random_content_redirect', 100 ); |
| * {@see WP::handle_random()} once the main query has selected the random post, so the | ||
| * redirect is sent after the request is set up and the {@see 'wp'} action has fired. |
…the random redirect. `redirect_canonical()` runs on `template_redirect` before `wp_random_content_redirect()`, so a request such as `/random/?m=1844` was redirected to `/1844/`, dropping the `random` query variable. Canonical redirects are now skipped for random content requests that have a post to redirect to. `wp_random_content_redirect()` now reads the selected post from the main query, so the redirect is still sent when an earlier callback replaces the current query, for example by calling `query_posts()`. See #66661. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>


The random content redirect was sent from
WP::handle_random()before thetemplate_redirectaction, so plugins restricting access to a site on that action never saw the request and logged-out visitors were redirected to post URLs. The redirect is now sent bywp_random_content_redirect()ontemplate_redirectat priority 100.redirect_canonical()no longer redirects random content requests that have a post to redirect to, so these requests are not redirected twice.Trac Ticket: https://core.trac.wordpress.org/ticket/66661
Use of AI Tools
AI assistance: Yes
Tool(s): Claude
Model(s): Fable 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.