Display a Tweet Button in WordPress
The Twitter plugin for WordPress supports generating a Tweet button through the twitter_share
WordPress shortcode. Add a Tweet button to a WordPress post by including the shortcode in your post content area or by evaluating a shortcode in PHP using the do_shortcode WordPress function.
Example:
Share this post: [twitter_share]
Supported shortcode parameters
Pre-populated text highlighted in the Tweet composer.
A text value provided in a shortcode overrides the text value provided in the post editor.
Example Value: Hello world
A comma-separated list of hashtags to include with the Tweet.
A hashtags value provided in a shortcode overrides the hashtags value provided in the post editor.
Example Value: cute,funny
URL included with the Tweet.
Automatically set to the permalink value for the post after passing through the twitter_url
filter when executed inside the loop.
Example Value: https://example.com/
Attribute the source of a Tweet to a Twitter username.
Defaults to the Twitter username provided in the Site Attribution section of the Twitter settings page. May be overridden by the twitter_via_username
filter.
Example Value: TwitterDev
Specify a Tweet ID for a new Tweet to appear in reply to another Tweet.
Example Value: 463440424141459456
A Tweet button displays the number of Tweets referencing the specified URL to the left or right of the button by default.
Set to none
to omit the Tweet count or set to vertical
to display the Tweet count above the button.
Example Value: none
Specify a count URL to be used to count the number of Tweets referencing your URL if you have modified your share URL.
Example Value: https://example.com/
Set to left
or right
to force align the button inside the generated iframe.
Example Value: right
Set to large
to display a larger version of the Tweet button.
Example Value: large
Site-wide customization using a filter
A website may programmatically set Tweet button parameters by acting on the associative array passed to the shortcode_atts_twitter_share
WordPress filter.
Functions acting on the filter should treat the value associated with the hashtags
key as a positional array of unique values. The value associated with the related
key should be treated as an associative array with keys of Twitter usernames and values describing each username (or an empty string if no description).
Example:
/** * Always show a large button * * @param array $options parsed options with defaults applied * @param array $defaults default values of a Tweet button * @param array $attributes user-defined shortcode attributes * * @return array options array with our customization applied */ function twitter_share_custom_options( $options, $defaults, $attributes ) { $options['size'] = 'large'; return $options; } add_filter( 'shortcode_atts_twitter_share', 'twitter_share_custom_options', 10, 3 );