WordPress.org

Ready to get started?Download WordPress

Forums

Mobile Redirect with a difference. (10 posts)

  1. gangstagoose
    Member
    Posted 5 months ago #

    Hi all!
    Firstly sorry if this is in the wrong section, feel free to move if need be!

    OK my issue, I have a wwordpress site running 1 theme. Now the desktop homepage utilises a video background and nothing else.

    The mobile homepage has a background image and various other content.

    What I need is a way of re-directing users to site.com/home if they view the homepage and are using a mobile device.

    All other pages can be viewed as normal.

    I know there are various plugins but I have tried ALOT and they all break the themes page builder.

    I know this is possible with php but can someone please explain to me in leymans terms what code to put where!

    Thanks in advance!

  2. gangstagoose
    Member
    Posted 5 months ago #

    Just to be clear, I do not want a mobile user to be redirected if they visit any page other than the home page.

  3. SpankMarvin
    Member
    Posted 5 months ago #

    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' );
  4. SpankMarvin
    Member
    Posted 5 months ago #

    Sorry, and place this in your functions.php file.

  5. gangstagoose
    Member
    Posted 5 months ago #

    Hiya!

    Thanks for your help!

    Do I replace "mobile_redirect" with the mobile homepage url?

    Noob question, does it matter where in functions.php I place it?

  6. SpankMarvin
    Member
    Posted 5 months ago #

    It shouldn't matter where you place it in functions.php.

    In the page editor, there should be a "Custom Fields" box. The name should be mobile_redirect and the value should be the URL.

    Don't forget you'll need a function for actually checking if mobile. I had no idea there is actually a WordPress function for this, wp_is_mobile. So I'd modify the function to be:

    function do_home_redirect() {
    
    	if( !is_front_page() ){
    		return;
    	}
    	if( wp_is_mobile() ){
    		$redirect = get_post_meta( $post->ID, "mobile_redirect", true );
    		if( $redirect ) {
    			header('Location: ' . esc_url( $redirect ));
    			exit;
    		}
    	}
    }
    
    add_action( 'init', 'do_home_redirect' );
  7. SpankMarvin
    Member
    Posted 5 months ago #

    It shouldn't matter where you place it in functions.php.

    In the page editor, there should be a "Custom Fields" box. The name should be mobile_redirect and the value should be the URL.

    Don't forget you'll need a function for actually checking if mobile. I had no idea there is actually a WordPress function for this, wp_is_mobile. So I'd modify the function to be:

    function do_home_redirect() {
    
    	if( !is_front_page() ){
    		return;
    	}
    	if( wp_is_mobile() ){
    		$redirect = get_post_meta( $post->ID, "mobile_redirect", true );
    		if( $redirect ) {
    			header('Location: ' . esc_url( $redirect ));
    			exit;
    		}
    	}
    }
    
    add_action( 'init', 'do_home_redirect' );
  8. gangstagoose
    Member
    Posted 5 months ago #

    Thanks again, I should point out I am a complete noob to this!

    I have copied the above code to functions.php

    The page builder has no custom fields box :(

    Is there a way to specify the home page within the php?

    Is there anything else I need to do? Sorry to be so vague but I really am lost in the fog with this!

  9. SpankMarvin
    Member
    Posted 5 months ago #

    Although it's better to have the URL being pulled from the CMS rather than hard-coded, if you must:

    (This assumes the mobile URL is http://google.com)

    function do_home_redirect() {
    
    	if( wp_is_mobile() && is_front_page() ){
    			header('Location: http://google.com/');
    			exit;
    	}
    
    }
    
    add_action( 'init', 'do_home_redirect' );
  10. gangstagoose
    Member
    Posted 5 months ago #

    I added the code above (the last reply only) to functions.php and left the url as google to test.

    Using a Galazy S4 mini to visit the home page I still see the sites homepage as normal rather than google.

    Is there anything else I need to add/do?

  11. SpankMarvin
    Member
    Posted 5 months ago #

    Try this code instead?

    function do_home_redirect() {
    
    	if( wp_is_mobile() && ( is_front_page() || is_home() ) ){
    			header('Location: http://google.com/');
    			exit;
    	}
    
    }
    
    add_action( 'init', 'do_home_redirect' );

Reply

You must log in to post.

About this Topic