How to show Twitter followers and more in WordPress
We've written a piece of code before that lets you show the number of Twitter followers, which is contributed by Rarst. In this article, we will share a more advanced and elegant code that allows you to display Twitter followers in WordPress. This script was also contributed by Rust.
feature
This feature is not limited to the number of followers. It can get any non-nested values returned by the Twitter users/show API method.
It has two levels of cache:
- The query's value is stored in the database as an array using the WP option, lasting $interval seconds;
- API responses are stored in memory, so you can query as many fields as you like without generating multiple API requests.
This should be safe to use both multiply values and multiply users without worrying about running out of API limits.
Tutorial
First open the functions.php file of the theme and add the following code:
functionrarst_twitter_user( $username, $field, $display= false ) {$interval= 3600;$cache= get_option('rarst_twitter_user');$url= 'http://api.twitter.com/1/users/show.json?screen_name='.urlencode($username);if( false == $cache)$cache= array();// if first time request add placeholder and force updateif( !isset( $cache[$username][$field] ) ) {$cache[$username][$field] = NULL;$cache[$username]['lastcheck'] = 0;}// if outdatedif( $cache[$username]['lastcheck'] $value)if( isset($data->$key) )$cache[$username][$key] = $data->$key;$cache[$username]['lastcheck'] = time();}else{$cache[$username]['lastcheck'] = time() 60;}update_option( 'rarst_twitter_user', $cache);}if( false != $display)echo$cache[$username][$field]; return$cache[$username][$field];}
Depend on
Use it with one click in WordPress
usage
After pasting the function, you can now use the code in any WordPress template file you like. Just paste the following code:
echoorarst_twitter_user('wpbeginner', 'name').' has '.rarsst_twitter_user('wpbeginner', 'followers_count').' followers after '.rarsst_twitter_user('wpbeginner', 'statuses_count').' updates.';
Depend on
Use it with one click in WordPress
The above code will display the following:
WPBeginner has 5846 followers after 1300 updates.
The above is the detailed content of How to show Twitter followers and more in WordPress. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

There are four ways to adjust the WordPress article list: use theme options, use plugins (such as Post Types Order, WP Post List, Boxy Stuff), use code (add settings in the functions.php file), or modify the WordPress database directly.

Cryptocurrency data platforms suitable for beginners include CoinMarketCap and non-small trumpet. 1. CoinMarketCap provides global real-time price, market value, and trading volume rankings for novice and basic analysis needs. 2. The non-small quotation provides a Chinese-friendly interface, suitable for Chinese users to quickly screen low-risk potential projects.

WordPress editing dates can be canceled in three ways: 1. Install the Enable Post Date Disable plug-in; 2. Add code in the functions.php file; 3. Manually edit the post_modified column in the wp_posts table.

A step-by-step guide to replacing a header image of WordPress: Log in to the WordPress dashboard and navigate to Appearance >Theme. Select the topic you want to edit and click Customize. Open the Theme Options panel and look for the Site Header or Header Image options. Click the Select Image button and upload a new head image. Crop the image and click Save and Crop. Click the Save and Publish button to update the changes.

To build a website using WordPress hosting, you need to: select a reliable hosting provider. Buy a domain name. Set up a WordPress hosting account. Select a topic. Add pages and articles. Install the plug-in. Customize your website. Publish your website.

The steps to create a custom header in WordPress are as follows: Edit the theme file "header.php". Add your website name and description. Create a navigation menu. Add a search bar. Save changes and view your custom header.

The future of Bitcoin: a medium of exchange or a store of value? The debate continues to ferment in the core Bitcoin community, and the latest remarks by Twitter co-founder and BlockInc. CEO Jack Dorsey have sparked new discussions. Is Bitcoin a payment tool, a storage means of value, or both? This issue has always been the focus of fierce debate among industry insiders. Dorsey's recent remarks have ignited the debate again. Dispute about the use of Bitcoin Dorsey believes that if Bitcoin is stored as a store of value only, it will fail. He pointed out in a recent podcast that Bitcoin’s success lies in its payment capabilities. This view is in contrast to the current major narrative of Bitcoin. Many people, including Wink

Cryptocurrency market turmoil has intensified, and Cardano (ADA) has fallen below key support levels, raising concerns among investors. This article will analyze the recent performance of ADA and compare the strong growth momentum of another cryptocurrency, Coldware (COLD). ADA prices have continued to decline in the past five months, falling below the $0.61 support level on April 7, 2025, triggering volatility in the overall cryptocurrency market. At the same time, Coldware (COLD)'s pre-sale performed well, with financing of US$2.4 million in the second phase. So, what are the advantages of Coldware compared to Cardano? Coldware rises against the trend. In the same market environment where ADA prices fall, Coldware(C
