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.

Called by WP::handle_random immediately after the main query selects the random post, to avoid unnecessary processing before the redirect. As the redirect is sent before WP::send_headers, the headers preventing the redirect being cached or indexed are sent with it. If no post is selected, for example on an empty archive, WP::handle_random handles the request as a 404. If the post's permalink is on another site, the request is handled as a regular archive request.


Quellcode

function wp_random_content_redirect(): void {
	global $wp_query;

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

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

	// Only consider redirecting permitted locations so wp_safe_redirect doesn't redirect to the fallback.
	if ( ! $location || ! wp_validate_redirect( $location, '' ) ) {
		return;
	}

	nocache_headers();

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

	if ( wp_safe_redirect( $location, 302 ) ) {
		exit;
	}
}