1

How do I echo a different title to the page if it's opened on an android mobile phone?

I have this function that checks if it's an android:

function is_android (){
    $browserInfo = php_browser_info();
    if (preg_match("/Android/", $browserInfo['browser_name_pattern'], $matches)) {
        if ($version == '') :
            return true;
        elseif ($browserInfo['majorver'] == $version ) :
            return true;
        else :
            return false;
        endif;
    } else {
        return false;
    }
}

And I can use the statement:

    add_action( 'init', 'change_title_if_android');
    function change_title_if_android() {
        if ( is_front_page() &&  is_android() ){
            echo "<title> test android </title>";
        }
    }

But this doesn't work. It prevents my page from loading.

I tried writing the if statement on the template too (header.php in the tag) and I got the same result.

Any suggestions ?

Edit:

I use the TwentyFifteen child theme I made.

I tried pasting this code part to function.php suggested by the commenter @birgire:

add_filter( 'wp_title', function( $title )
{
    return
        is_front_page()
        && isset( $_SERVER['HTTP_USER_AGENT'] ) 
        && false !== stripos( $_SERVER['HTTP_USER_AGENT'], 'android' )
        ? 'Test Android'
        : $title;
}, 11 );

He tested it on his 2012 & 2015 default themes and it worked for him!

It didn't work for me though, so I tried using is_home instead of is_front_page(), nothing changed.

I tried to remove this line and left only the the HTTPS_USER_AGENT, didn't work too.

We tested $_SERVER['HTTP_USER_AGENT'] using this:

add_action( 'template_redirect', function()
{
    if( filter_input( INPUT_GET, 'wpse_show_ua', FILTER_SANITIZE_NUMBER_INT ) )
    {
        printf( 'My User Agent: %s', $_SERVER['HTTP_USER_AGENT'] );
        exit;
    }
} );

and it worked. I got the right output when running example.tld/?wpse_show_ua=1 :

My User Agent: Mozilla/5.0 (Linux; Android 4.4.2; SM-G900F Build/KOT49H) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.93 Mobile Safari/537.36

We tested the functioanlity of the if statements and got it right:

add_action( 'template_redirect', function()
{
    if( filter_input( INPUT_GET, 'wpse_show_ua', FILTER_SANITIZE_NUMBER_INT ) )
    {
        if ( 
               isset( $_SERVER['HTTP_USER_AGENT'] ) 
            && false !== stripos( $_SERVER['HTTP_USER_AGENT'], 'android' ) 
        )
            echo 'is an Android!';
        else
            echo 'not an Android!';
        exit;
    }
} );

On a mobile 'is an Android' and on the desktop 'not an Android!'.

So the problem is actually with sending the new value of the title!

When running this function:

add_filter( 'wp_title', function( $title )
{
    if( filter_input( INPUT_GET, 'wpse_show_ua', FILTER_SANITIZE_NUMBER_INT ) )
    {
        return 'Testing 123';
    }
    return $title;
}, 11 );

and entering example.tld/?wpse_show_ua=1 againg, I get my regular website display with no changes in the title!

And again - this worked on the commenter default TwentyTwelve and TwentyFifteen themes.

My guess is that it's because of the plugin that is installed (Yoast SEO). but I really dont know why this is happening.

2

I'm not sure where this php_browser_info() function comes from.

You can try the following instead:

/**
 * Modify the front/home page title for Android devices
 */
add_filter( 'wp_title', function( $title )
{
    return
        ( is_front_page() || is_home() )
        && isset( $_SERVER['HTTP_USER_AGENT'] ) 
        && false !== stripos( $_SERVER['HTTP_USER_AGENT'], 'android' )
        ? 'Test Android'
        : $title;
}, PHP_INT_MAX );

to modiy the title of the front page. Here we use a modification of the Android check from the wp_is_mobile() function, so please note that this detection might not be that accurate!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.