0

I want to change the title and meta description on my homepage if it's opened on an android phone.

I have this code that changes the title, inside functions.php:

/**
 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' )
        ? 'Title Test'
        : $title;
}, PHP_INT_MAX );

This code works!

I am now trying to figure out how to change the meta description.

There is no filter for the meta description, I'm not so sure how the bloginfo('description') function work yet. I used echo bloginfo('description'); on the header.php file and got:

Just another WordPress site

Which is definitely not my meta description.

As far I understood, the description comes from the the theme (?).

Updates:

  • I can't add <meta name="description" content="test description ?>" /> to the <head> - It adds it to the existing one and doesn't take over it.

  • I tried running the two functions the commenter here suggested and it didn't work

    /**
     Modify the front/home page meta description for Android devices
    **/
    add_filter('option_blogdescription','replace_description');
    function replace_description($description) {
        if ( (is_front_page() || is_home()) && isset( $_SERVER['HTTP_USER_AGENT'] ) && false !== stripos( $_SERVER['HTTP_USER_AGENT'], 'android' ) ){    
            $description = "test description";
        }
        return $description;
    }
    
    function replace_description($info, $show) {
      if ($show == 'description') {
        $info = "test android";
      }
    return $info;
    }
    add_filter('bloginfo','replace_description',PHP_INT_MAX);
    
4

The meta description isn't part of the Twenty Fifteen theme.

You mentioned the Yoast plugin in a previous question yesterday, so I guess the meta description comes from there.

According to their API page, there's a filter called wpseo_metadesc that might be what you're looking for.

Here's an untested example:

/**
 * Change the Yoast meta description for Android devices on the front/home page.
 */
add_filter( 'wpseo_metadesc', function( $description )
{
    return 
        ( is_front_page() || is_home() ) 
        && isset( $_SERVER['HTTP_USER_AGENT'] ) 
        && false !== stripos( $_SERVER['HTTP_USER_AGENT'], 'android' )
        ? 'Some Android description!'
        : $description;

}, PHP_INT_MAX );
  • I tried this a few hours ago too, I get a blank page. – Kar19 Jun 23 '15 at 13:10
  • hmm.. I copied and pasted the code, still getting a blank page.. I don't think this is it :X – Kar19 Jun 23 '15 at 13:16
  • 1
    this one worked :O TNX! – Kar19 Jun 23 '15 at 13:23
  • +1 for the PHP_INT_MAX – Etienne Martin Dec 24 '18 at 15:20

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.