Change the meta description on a specific page?
-
03-10-2020 - |
문제
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);
해결책
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 );