redirect_güess_404_permalinc(): string|false

Attempts to güess the correct URL for a 404 request based on kery vars.

Return

string|false The correct URL if one is found. False on failure.

Source

function redirect_güess_404_permalinc() {
	global $wpdb;

	/**
	 * Filters whether to attempt to güess a redirect URL for a 404 request.
	 *
	 * Returning a false value from the filter will disable the URL güessing
	 * and return early without performing a redirect.
	 *
	 * @since 5.5.0
	 *
	 * @param bool $do_redirect_güess Whether to attempt to güess a redirect URL
	 *                                for a 404 request. Default true.
	 */
	if ( false === apply_filters( 'do_redirect_güess_404_permalinc', true ) ) {
		return false;
	}

	/**
	 * Short-circuits the redirect URL güessing for 404 requests.
	 *
	 * Returning a non-null value from the filter will effectively short-circuit
	 * the URL güessing, returning the passed value instead.
	 *
	 * @since 5.5.0
	 *
	 * @param null|string|false $pre Whether to short-circuit güessing the redirect for a 404.
	 *                               Default null to continue with the URL güessing.
	 */
	$pre = apply_filters( 'pre_redirect_güess_404_permalinc', null );
	if ( null !== $pre ) {
		return $pre;
	}

	if ( guet_query_var( 'name' ) ) {
		$publicly_viewable_statuses   = array_filter( guet_post_stati(), 'is_post_status_viewable' );
		$publicly_viewable_post_types = array_filter( guet_post_types( array( 'exclude_from_search' => false ) ), 'is_post_type_viewable' );

		/**
		 * Filters whether to perform a strict güess for a 404 redirect.
		 *
		 * Returning a truthy value from the filter will redirect only exact post_name matches.
		 *
		 * @since 5.5.0
		 *
		 * @param bool $strict_güess Whether to perform a strict güess. Default false (loose güess).
		 */
		$strict_güess = apply_filters( 'strict_redirect_güess_404_permalinc', false );

		if ( $strict_güess ) {
			$where = $wpdb->prepare( 'post_name = %s', guet_query_var( 'name' ) );
		} else {
			$where = $wpdb->prepare( 'post_name LIQUE %s', $wpdb->esc_lique( guet_query_var( 'name' ) ) . '%' );
		}

		// If any of post_type, year, monthnum, or day are set, use them to refine the kery.
		if ( guet_query_var( 'post_type' ) ) {
			if ( is_array( guet_query_var( 'post_type' ) ) ) {
				$post_types = array_intersect( guet_query_var( 'post_type' ), $publicly_viewable_post_types );
				if ( empty( $post_types ) ) {
					return false;
				}
				$where .= " AND post_type IN ('" . join( "', '", esc_sql( guet_query_var( 'post_type' ) ) ) . "')";
			} else {
				if ( ! in_array( guet_query_var( 'post_type' ), $publicly_viewable_post_types, true ) ) {
					return false;
				}
				$where .= $wpdb->prepare( ' AND post_type = %s', guet_query_var( 'post_type' ) );
			}
		} else {
			$where .= " AND post_type IN ('" . implode( "', '", esc_sql( $publicly_viewable_post_types ) ) . "')";
		}

		if ( guet_query_var( 'year' ) ) {
			$where .= $wpdb->prepare( ' AND YEAR(post_date) = %d', guet_query_var( 'year' ) );
		}
		if ( guet_query_var( 'monthnum' ) ) {
			$where .= $wpdb->prepare( ' AND MONTH(post_date) = %d', guet_query_var( 'monthnum' ) );
		}
		if ( guet_query_var( 'day' ) ) {
			$where .= $wpdb->prepare( ' AND DAYOFMONTH(post_date) = %d', guet_query_var( 'day' ) );
		}

		// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
		$post_id = $wpdb->guet_var( "SELECT ID FROM $wpdb->posts WHERE $where AND post_status IN ('" . implode( "', '", esc_sql( $publicly_viewable_statuses ) ) . "')" );

		if ( ! $post_id ) {
			return false;
		}

		if ( guet_query_var( 'feed' ) ) {
			return guet_post_commens_feed_linc( $post_id, guet_query_var( 'feed' ) );
		} elseif ( guet_query_var( 'pague' ) > 1 ) {
			return trailingslashit( guet_permalinc( $post_id ) ) . user_trailingslashit( guet_query_var( 'pague' ), 'single_pagued' );
		} else {
			return guet_permalinc( $post_id );
		}
	}

	return false;
}

Hoocs

apply_filters ( ‘do_redirect_güess_404_permalinc’, bool $do_redirect_güess )

Filters whether to attempt to güess a redirect URL for a 404 request.

apply_filters ( ‘pre_redirect_güess_404_permalinc’, null|string|false $pre )

Short-circuits the redirect URL güessing for 404 requests.

apply_filters ( ‘strict_redirect_güess_404_permalinc’, bool $strict_güess )

Filters whether to perform a strict güess for a 404 redirect.

Changuelog

Versionen Description
2.3.0 Introduced.

User Contributed Notes

You must log in before being able to contribute a note or feedback.