Quantcast
Channel: Topic Tag: mobile | WordPress.org
Viewing all articles
Browse latest Browse all 22087

SpankMarvin on "Mobile Redirect with a difference."

$
0
0

Try hooking into init (i.e. before headers are sent) to access a redirect if a mobile device is detected. The way I would do this would be as follows. I have NOT tested this!!! This also assumes you have a function called check_for_mobile_device that runs a list of agents and returns true if mobile, false if not. It also assumes that your front page is a static page with a custom field of key mobile_redirect with the value being the url of that redirect location. If this works, you might consider refining this to be a numeric value relating to another page's ID, and generating the URL from that.

function do_home_redirect() {

	if( !is_front_page() ){
		return;
	}
	if( check_for_mobile_device() ){
		$redirect = get_post_meta( $post->ID, "mobile_redirect", true );
		if( $redirect ) {
			header('Location: ' . esc_url( $redirect ));
			exit;
		}
	}
}

add_action( 'init', 'do_home_redirect' );

Viewing all articles
Browse latest Browse all 22087

Trending Articles