Skip to content

Query: Send the random content redirect on template_redirect. - #15912

Open
aaronjorbin wants to merge 3 commits into
WordPress:trunkfrom
aaronjorbin:try/move-random-redirect-to-tempalte-redirect
Open

aaronjorbin wants to merge 3 commits into
WordPress:trunkfrom
aaronjorbin:try/move-random-redirect-to-tempalte-redirect

Conversation

@aaronjorbin

Copy link
Copy Markdown
Member

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

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.

aaronjorbin and others added 2 commits November 11, 2026 14:49
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
@github-actions

Copy link
Copy Markdown

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:

Props jorbin.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.


Props Bot commands

You can trigger Props Bot actions by commenting on this PR:

  • @propsbot add <username> to give props to a GitHub user. Requires write access.
  • @propsbot remove <username> to leave a user out of the props. Requires write access.
  • @propsbot refresh to refresh this list. Adding the props-bot label works too.

@github-actions

Copy link
Copy Markdown

Test using WordPress Playground

The 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

  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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 );
Comment thread src/wp-includes/query.php
Comment on lines +1468 to +1469
* {@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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants