wpseek.com
A WordPress-centric search engine for devs and theme authors
wp_random_content_redirect › WordPress Function
Since7.2.0
Deprecatedn/a
› wp_random_content_redirect ( No parameters )
| Defined at: |
|
| Codex: |
Redirects requests for random content to a randomly selected published post.
Handles requests using therandom query parameter, e.g. example.com/?random.
The random_post_type parameter restricts the pick to a post type (default post),
and the random_cat_id parameter restricts it to a category (for post types using
the category taxonomy). Only published, non-password-protected content from
viewable post types is considered, and only GET/HEAD requests are handled.
Disabled by default. To enable:
add_filter( 'enable_random_content_redirect', '__return_true' );Source
function wp_random_content_redirect() {
// The `random` parameter is a flag, present with or without a value.
if ( ! isset( $_GET['random'] ) ) {
return;
}
/**
* Filters whether random content redirects are enabled.
*
* @since 7.2.0
*
* @param bool $enabled Whether `?random` requests redirect to random content. Default false.
*/
if ( ! apply_filters( 'enable_random_content_redirect', false ) ) {
return;
}
$request_method = isset( $_SERVER['REQUEST_METHOD'] ) ? strtoupper( $_SERVER['REQUEST_METHOD'] ) : '';
if ( ! in_array( $request_method, array( 'GET', 'HEAD' ), true ) ) {
return;
}
$post_type = 'post';
if ( isset( $_GET['random_post_type'] ) ) {
$post_type = sanitize_key( wp_unslash( $_GET['random_post_type'] ) );
}
$post_type_object = get_post_type_object( $post_type );
if ( ! $post_type_object || ! is_post_type_viewable( $post_type_object ) ) {
return;
}
$args = array(
'post_type' => $post_type,
'post_status' => 'publish',
'has_password' => false,
'posts_per_page' => 1,
'orderby' => 'ID',
'order' => 'ASC',
'ignore_sticky_posts' => true,
'update_post_term_cache' => false,
'update_post_meta_cache' => false,
);
if ( isset( $_GET['random_cat_id'] ) ) {
$cat_id = (int) wp_unslash( $_GET['random_cat_id'] );
if ( $cat_id < 1 ) {
return;
}
$args['cat'] = $cat_id;
}
// Pick via COUNT plus a random OFFSET rather than `ORDER BY RAND()`,
// which randomizes and sorts every candidate row on each request.
$query = new WP_Query( $args );
$post_count = (int) $query->found_posts;
if ( $post_count > 1 ) {
$offset = wp_rand( 0, $post_count - 1 );
if ( $offset > 0 ) {
$args['offset'] = $offset;
$args['no_found_rows'] = true;
$args['cache_results'] = false;
$query = new WP_Query( $args );
}
}
if ( empty( $query->posts ) ) {
return;
}
$link = get_permalink( $query->posts[0] );
/**
* Filters the random content redirect URL.
*
* Returning a falsey value cancels the redirect.
*
* @since 7.2.0
*
* @param string $link The redirect URL.
*/
$link = apply_filters( 'random_content_redirect_url', $link );
if ( ! $link ) {
return;
}
// Temporary redirect, so clients and intermediary caches do not cache the randomly picked target.
nocache_headers();
wp_safe_redirect( $link, 302 );
exit;
}