The Yoast SEO plugin registers some helpful Social Media contact methods on a WordPress User Profile that are available for users to add information for Google+, Twitter, and Facebook.
You can access the values set for each user on the front-end of your WordPress site using the get_the_author_meta() function.
However, when creating an Author template in a theme or for your project, you’ll possibly never know exactly what meta you should be looking for to output. Using the “get_the_author_meta()” approach assumes that we’ll know the exact fields to display even though an unlimited number of options can be added via other plugins or another theme.
In this scenario, WordPress has a very helpful function called “wp_get_user_contact_methods()” which will list all the possible contact methods available on the site and allow you to query the user for each available method.
So, why can’t we do that with the Yoast contact methods?
If you’re using Yoast and have added information to those fields on your profile, try running the “wp_get_user_contact_methods()” and see what gets returned on your front-end template.
They’re not there.
The reason is that Yoast runs the filter for these only within the admin of your site. So, you can see them within the profile but not listed as options on the front-end there.
The below snippet will re-register the Yoast contact methods and make them available from anywhere in your site.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_filter( 'user_contactmethods', 'limecuda_public_yoast_contactmethods', 20, 1 ); | |
/** | |
* If you're using the Yoast SEO plugin, there are contact methods available on user profiles | |
* to add Facebook, Twitter, and Google+ information for the user. | |
* | |
* However, with the way they are added, they're only available within the admin. So, | |
* if you're trying to create an author template which automatically pulls all the registered | |
* contact methods, these Yoast methods will not be returned. | |
* | |
* This function will make these contact methods available to wp_get_user_contact_methods(). | |
* | |
* @param array $methods The registered contact methods. | |
*/ | |
function limecuda_public_yoast_contactmethods( $methods ) { | |
if ( ! function_exists( 'wpseo_init' ) ) | |
return $methods; | |
$methods['googleplus'] = __( 'Google+', 'wordpress-seo' ); | |
$methods['twitter'] = __( 'Twitter username (without @)', 'wordpress-seo' ); | |
$methods['facebook'] = __( 'Facebook profile URL', 'wordpress-seo' ); | |
return $methods; | |
} |