Family Best Time >> Electronics

How to display the number of Twitter followers as text in WordPress

The easiest way to display Twitter followers is by using the official Twitter follow button. But what if you don't want to slow down your site by loading the Twitter script? Or, if you're doing something very custom and need to display the number of Twitter followers as text instead of a button. Well then you will like this tutorial. In this article, we'll show you how to display your Twitter follower count as text on your WordPress site.

Wondering how we're going to do this? Well, first we will create a Twitter application, so that we can correctly use the Twitter API v1.1 to get the number of followers. We'll cache it for performance optimization, and then display it on the site. Ready to start? Let's go.

The first thing you need to do is create a Twitter app for the site where you want to display the number of followers. Go to the Twitter developer website and sign in with your Twitter account. After login create a new app.

How to display the number of Twitter followers as text in WordPress

On the next screen, provide a name for your app, this could be anything, ideally the title of your website. Provide a description for your app, this could be the same description as your blog or anything you like. In the website field, enter the URL of your WordPress site, for example:https://www.wpbeginner.com.

Enter the same URL in the Callback URL field as well. After filling in the form press the Create your Twitter application button at the bottom of the page.

This will create a new Twitter app for you to use. On the next page, click Create my access token button. This will show you a notification that your authorization token has been created.

On your Twitter app page, we'll just need the Consumer Key and Consumer Secret for the next step.

Copy the following code and paste it into your theme funciones.php file or a site-specific plugin. Replace the Consumer Key and Consumer Secret variables with your consumer key and consumer secret.

 function getTwitterFollowers ($ screenName = 'wpbeginner') // algunas variables $ consumerKey = 'YOUR_CONSUMER_KEY'; $ consumerSecret = 'YOUR_CONSUMER_SECRET'; $ token = get_option ('cfTwitterToken'); // obtener el número de seguidores de la caché $ numberOfFollowers = get_transient ('cfTwitterFollowers'); // la versión de caché no existe o caducó si (false === $ numberOfFollowers) // obteniendo un nuevo portador de autenticación solo si no tenemos uno if (! $ token) // preparando credenciales $ credentials = $ consumerKey. ':'. $ consumerSecret; $ toSend = base64_encode ($ credenciales); // argumentos de la publicación http $ args = array ('method' => 'POST', 'httpversion' => '1.1', 'bloqueo' => true, 'headers' => array ('Autorización' => 'Básico' . $ toSend, 'Content-Type' => 'application / x-www-form-urlencoded; charset = UTF-8'), 'body' => array ('grant_type' => 'client_credentials')); add_filter ('https_ssl_verify', '__return_false'); $ response = wp_remote_post ('https://api.twitter.com/oauth2/token', $ args); $ keys = json_decode (wp_remote_retrieve_body ($ response)); if ($ keys) // guardando el token en wp_options table update_option ('cfTwitterToken', $ keys-> access_token); $ token = $ keys-> access_token; // tenemos un token de soporte, ya sea que lo hayamos obtenido de API o de options $ args = array ('httpversion' => '1.1', 'bloqueo' => true, 'headers' => array ('Autorization' => "Token $ token")); add_filter ('https_ssl_verify', '__return_false'); $ api_url = "https://api.twitter.com/1.1/users/show.json?screen_name=$screenName"; $ respuesta = wp_remote_get ($ api_url, $ args); if (! is_wp_error ($ response)) $ followers = json_decode (wp_remote_retrieve_body ($ response)); $ numberOfFollowers = $ followers-> followers_count; else // obtiene el valor antiguo y rompe $ numberOfFollowers = get_option ('cfNumberOfFollowers'); // descomprima a continuación para depurar // die ($ response-> get_error_message ()); // caché por una hora set_transient ('cfTwitterFollowers', $ numberOfFollowers, 1 * 60 * 60); update_option ('cfNumberOfFollowers', $ numberOfFollowers); devolver $ numberOfFollowers; 

Now add this line of code in your theme template where you want to display the number of your Twitter followers. This could be in the sidebar.php, header.php, or basically anywhere you want.

 

That's it. Are you ready. We hope this article helped you display Twitter followers as text in WordPress. There are many other things you can do to integrate Twitter with your WordPress site. For example, you can add twitter cards or display recent tweets in WordPress. For more helpful tips, consider following @wpbeginner on Twitter.

Source:Zvonko Biskup