Make WordPress Core

Opened 8 months ago

Last modified 5 weeks ago

#64498 new feature request

Add core support for random content redirects

Reported by: annezazu Owned by:
Priority: normal Milestone: Awaiting Review
Component: Rewrite Rules Version:
Severity: normal Keywords: has-patch dev-feedback has-unit-tests
Cc: Focuses:

Description

WordPress Core does not include functionality to redirect a request to a randomly selected post or page. This behavior is currently implemented through plugins or custom code. This ticket proposes discussing whether a minimal, opt-in core implementation should exist. Any implementation could rely on existing query and rewrite APIs without introducing user-facing settings or defaults. @matt recently mentioned this as something to consider for 7.0 based on his early plugin here: https://wordpress.org/plugins/random-redirect/

Attachments (1)

64498-random-content-redirect.patch (12.5 KB ) - added by solankisoftware 8 months ago.

Download all attachments as: .zip

Change History (6)

#1 @solankisoftware
8 months ago

This would be very useful for content discovery, learning sites, and blogs. A minimal rewrite + redirect approach without UI would be ideal and backward-compatible.

#2 @solankisoftware
8 months ago

  • Keywords has-patch added

#3 @solankisoftware
8 months ago

  • Keywords needs-testing needs-unit-tests added

#4 @sajib1223
7 months ago

  • Keywords dev-feedback added; needs-testing removed
  • Type enhancementfeature request

Latest patch (https://core.trac.wordpress.org/attachment/ticket/64498/64498-random-content-redirect.patch) does not apply cleanly against trunk.

Running "patch:64498" (patch) task
patch unexpectedly ends in middle of line
patch: **** Only garbage was found in the patch input.

Additionally, the patch appears to bundle unrelated docblock/documentation changes alongside the new feature, which should ideally be separated.

Finally, as this is a new functionality, changed the type to feature request.

This ticket was mentioned in PR #13022 on WordPress/wordpress-develop by @mlaetitia.


5 weeks ago
#5

  • Keywords has-unit-tests added; needs-unit-tests removed

This PR adds a minimal, opt-in implementation of random content redirects, superseding the earlier patch on the ticket (which no longer applies cleanly and bundled unrelated docblock changes).

## What it does

  • Adds wp_random_content_redirect() (in wp-includes/query.php, alongside its closest sibling wp_old_slug_redirect()), hooked on template_redirect.
  • When enabled, a request to example.com/?random issues a 302 redirect to a randomly selected published post. ?random&random_post_type=page restricts the pick to a post type (viewable types only); ?random&random_cat_id=<term ID> restricts it to a category. These parameter names match Matt's original Random Redirect plugin referenced in the ticket, so existing ?random links keep working.
  • Disabled by default — per the ticket's request for an opt-in implementation with no user-facing settings or defaults, nothing happens unless a theme or plugin opts in:
add_filter( 'enable_random_content_redirect', '__return_true' );
  • A second filter, random_content_redirect_url, allows short-circuiting or rewriting the redirect target (same pattern as old_slug_redirect_url).

## Implementation notes

  • The random pick uses a COUNT plus a random OFFSET (two WP_Query calls) rather than ORDER BY RAND(), which randomizes and sorts every candidate row on each request and is uncached.
  • Password-protected and non-published posts are never redirect targets. Non-viewable post types are rejected. Only GET/HEAD requests are handled.
  • The redirect is a 302 so clients do not cache the randomly picked target.
  • Open question for review: the parameters are read from $_GET on template_redirect, matching the original plugin's addressing. If pretty-URL support (e.g. a /random/ rewrite endpoint) is desirable, the parameters should instead become registered query vars — happy to rework in that direction if preferred.

## Tests

tests/phpunit/tests/rewrite/randomContentRedirect.php covers: disabled-by-default behavior, no-op without the random parameter, redirect to a published post, post type and category restriction, exclusion of password-protected/draft/private posts, invalid and non-viewable post types, nonexistent categories, empty result sets, and non-GET requests.

## Use of AI Tools

AI assistance: Yes
Tool(s): Claude Code
Model(s): Claude Fable 5
Used for: Initial implementation and test suite, ported from the Jetpack Random Redirect module under my direction; reviewed, tested, and edited by me.

Note: See TracTickets for help on using tickets.