wpseek.com
Eine auf WordPress spezialiserte Suchmaschine für Entwickler und Theme-Autoren



wp_random_content_redirect › WordPress Function

Seit7.2.0
Veraltetn/v
› wp_random_content_redirect ( Keine Parameter )
Definiert in:
Codex:

Redirects random content requests to the randomly selected post.

Added to the {@see 'template_redirect'} action at priority 100 by 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 headers preventing the redirect being cached or indexed are sent with it. The post is read from the main query rather than the current query, so the redirect is still sent when an earlier callback replaces the current query, for example by calling query_posts. The request ends once a post is selected. The redirect is to the post's permalink as returned by get_permalink, including a permalink filtered to be on another site. If no post is selected, for example on an empty archive, WP::handle_random handles the request as a 404.


Quellcode

function wp_random_content_redirect(): void {
	global $wp_the_query;

	if (
		! $wp_the_query instanceof WP_Query
		|| ! wp_is_random_content_query( $wp_the_query )
		|| ! $wp_the_query->post instanceof WP_Post
	) {
		return;
	}

	nocache_headers();

	if ( ! headers_sent() ) {
		header( 'X-Robots-Tag: noindex, follow' );
	}

	$location = get_permalink( $wp_the_query->post );

	wp_redirect( $location, 302 );
	exit;
}