Redirect_404_Vehicles::maybe_redirect( mixed $wp_obj )

maybe_redirect


Description Description

Checks to see if the request is for a vehicle that no longer exists. If it was, it decides where to redirect the user and performs that redirect.


Top ↑

Parameters Parameters

$wp_obj

(mixed) (Required)


Top ↑

Return Return

(void)


Top ↑

Source Source

File: includes/class-redirect-404-vehicles.php

		function maybe_redirect( $wp_obj ) {

			//is this a request for a vehicle?
			//is this a 404?
			if( ! $this->is_request_for_vehicle( $wp_obj ) || ! is_404() ) {
				return;
			}

			//base link to the inventory page
			$url = get_post_type_archive_link( INVP::POST_TYPE );

			//get the make out of the slug
			$make = $this->extract_make( $wp_obj );
			if( ! is_string( $make ) || '' == $make ) {
				//no make, redirect to vehicle archive
				wp_safe_redirect( $url, 302 );
				exit;
			}

			//are there cars in this make?
			$term = get_term_by( 'slug', $make, 'make' );
			if( ! $term || 0 == $term->count ) {
				//no cars in this make, go to vehicle archive
				wp_safe_redirect( $url, 302 );
				exit;
			}

			//redirect to this make's archive
			$url = get_term_link( $make, 'make' );
			if( is_wp_error( $url ) ) {
				//no link created for this make? go to vehicle archive
				wp_safe_redirect( $url, 302 );
				exit;
			}

			wp_safe_redirect( $url, 302 );
			exit;
		}