Make WordPress Core


Ignore:
Timestamp:
10/16/2026 05:03:59 PM (5 days ago)
Author:
jorbin
Message:

Rewrite Rules: Add support for random content redirects.

Introduce a random query var and /random/ rewrite rules[1], built on the existing rewrite, WP, and query APIs, that redirect to a randomly selected published post. Random requests[2] work for the whole site, taxonomy terms, authors and post type archives, with the base configurable on the Permalinks screen. A new randomable post type argument controls which content is eligible, and the selection runs through the main query so it remains filterable and costs a single database query.

The Random Base[3] is translatable and stored in the rewrite rules. This means that rewrite rules are regenerated when translations are updated or the Site Language is changed. Also, if a post or page is published that would result in the same slug as the Random Base, that content gets priority and nothing will be random[4].

This reintroduces the feature committed in r63767 and reverted in r63906.

1) Literally /random/, not just rules that are random. Though that could be fun too!
2) Not "random" as in unpredictable, but random as in someone wanted it to be random.
3) It's intentionally "random" but it could also intentionally be "aleatoria" or "karkovice".
4) The other random permalinks will still work, so not nothing. But is anything really random?

Developed in: ​https://github.com/WordPress/wordpress-develop/pull/14018

Follow-up to r63767, r63906.

Props jorbin, peterwilsoncc, westonruter, mukesh27, annezazu, mlaetitia, mcsf, johnbillion, TobiasBg, solankisoftware, sajib1223, ellatrix, matt.
See #64498.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/post.php

    r64388 r64402  
    2828                        ),
    2929                        'public'                => true,
     30                        'randomable'            => true,
    3031                        '_builtin'              => true, /* internal use only. don't use this when registering your own post type. */
    3132                        '_edit_link'            => 'post.php?post=%d', /* internal use only. don't use this when registering your own post type. */
    … …  
    6566                        'public'                => true,
    6667                        'publicly_queryable'    => false,
     68                        'randomable'            => false,
    6769                        '_builtin'              => true, /* internal use only. don't use this when registering your own post type. */
    6870                        '_edit_link'            => 'post.php?post=%d', /* internal use only. don't use this when registering your own post type. */
    … …  
    105107                        ),
    106108                        'public'                => true,
     109                        'randomable'            => false,
    107110                        'show_ui'               => true,
    108111                        '_builtin'              => true, /* internal use only. don't use this when registering your own post type. */
    … …  
    17261729 *                                                          * ?{post_type_query_var}={single_post_slug}
    17271730 *                                                         If not set, the default is inherited from $public.
     1731 *     @type bool         $randomable                      Whether posts of this post type can be selected by random content
     1732 *                                                         redirects, such as example.com/random/. If not set, the default is
     1733 *                                                         true when $publicly_queryable is true and $has_archive is set.
    17281734 *     @type bool         $show_ui                         Whether to generate and allow a UI for managing this post type in the
    17291735 *                                                         admin. Default is value of $public.
Note: See TracChangeset for help on using the changeset viewer.