Helper library for retrieving tweets from the Twitter API., (*1)
Installation
The library is available as Composer package. You can include it in your Project with:, (*2)
composer require htmlburger/carbon-twitter
, (*3)
Usage
use Carbon_Twitter\Carbon_Twitter;
Carbon_Twitter::config( array(
'api' => array(
'access_token' => '',
'access_token_secret' => '',
'consumer_key' => '',
'consumer_secret' => '',
),
'cache_lifetime' => 300,
'verbose' => false,
'cache_candidates' => [ 'WordPress', 'File' ],
) );
$tweets = Carbon_Twitter::get_tweets( 'wordpress', 5 );
foreach ( $tweets as $tweet ) {
echo $tweet->text;
}
or by using the helper functions:, (*4)
carbon_twitter_set_config( array(
'api' => array(
'access_token' => '',
'access_token_secret' => '',
'consumer_key' => '',
'consumer_secret' => '',
),
'cache_lifetime' => 300,
'verbose' => false,
'cache_drivers' => [ 'WordPress', 'File' ],
) );
$tweets = carbon_twitter_get_tweets( 'wordpress', 5 );
foreach ( $tweets as $tweet ) {
echo $tweet->text;
}
Configuration Parameters
api (array) - required
, (*5)
The api
parameter holds an array of 4 elements:, (*6)
access_token
access_token_secret
consumer_key
consumer_secret
cache_lifetime (int) - optional
, (*7)
The cache duration defined in seconds. Defaults to 300
seconds (5 minutes), (*8)
verbose (boolean) - optional
, (*9)
Whether to enable Verbose mode. Defaults to false
., (*10)
cache_drivers (array) - optional
, (*11)
An array of Cache Drivers to use. Uses the first Driver which is supported in the Project environment. Defaults to array( 'WordPress', 'File' )
, (*12)
In WordPress environment
Once the library is installed in your WordPress project, the receive the following features out of the box:, (*13)
- Carbon_Twitter_Feed widget is being registered
- A new Carbon Container is registered - Twitter Settings
There are several hooks that you can use in order to customize the functionality, (*14)
carbon_twitter_widget_id, (*15)
Allows you to modify the Widget ID., (*16)
apply_filters( 'carbon_twitter_widget_id', 'carbon_twitter_feed_widget' );
carbon_twitter_widget_title, (*17)
Allows you to modify the default Widget title., (*18)
apply_filters( 'carbon_twitter_widget_title', __( 'Carbon Twitter Feed', 'carbon-twitter' ) )
carbon_twitter_widget_description, (*19)
Allows you to modify the default Widget description., (*20)
apply_filters( 'carbon_twitter_widget_description', __( 'Displays a Twitter Feed.', 'carbon-twitter' ) )
carbon_twitter_widget_fields, (*21)
Allows you to modify the array of the default Widget fields., (*22)
apply_filters( 'carbon_twitter_widget_fields', array(
Field::make( 'text', 'title', __( 'Title', 'carbon-twitter' ) ),
Field::make( 'text', 'twitter_username', __( 'Twitter Username', 'carbon-twitter' ) ),
Field::make( 'text', 'count_tweets', __( 'Number of Tweets to Display', 'carbon-twitter' ) )
->set_default_value( 5 ),
) )
carbon_twitter_widget_classes, (*23)
Allows you to modify the CSS classes that will be added to the Widget., (*24)
apply_filters( 'carbon_twitter_widget_classes', 'carbon-twitter-feed' )
carbon_twitter_settings_title, (*25)
Allows you to change the title of the Twitter Settings Carbon Container., (*26)
apply_filters( 'carbon_twitter_settings_title', __( 'Twitter Settings', 'carbon-twitter' ) )
carbon_twitter_settings_page_parent, (*27)
Allows you to change the Page Parent of the Twitter Settings Carbon Container., (*28)
apply_filters( 'carbon_twitter_settings_page_parent', 'crbn-theme-options.php' )
carbon_twitter_settings_custom_help_text, (*29)
Allows you to modify the help text of the Twitter Settings Carbon Container., (*30)
carbon_twitter_settings_fields, (*31)
Allows you to modify the default fields in the Twitter Settings Carbon Container., (*32)
apply_filters( 'carbon_twitter_settings_fields', array(
Field::make( 'html', 'carbon_twitter_settings_html' )
->set_html( carbon_twitter_get_options_help_text() ),
Field::make( 'text', 'carbon_twitter_consumer_key', __( 'Consumer Key', 'carbon-twitter' ) ),
Field::make( 'text', 'carbon_twitter_consumer_secret', __( 'Consumer Secret', 'carbon-twitter' ) ),
Field::make( 'text', 'carbon_twitter_access_token', __( 'Access Token', 'carbon-twitter' ) ),
Field::make( 'text', 'carbon_twitter_access_token_secret', __( 'Access Token Secret', 'carbon-twitter' ) ),
) )