Below, you’ll find a detailed guide on how to add the Twitter Shortcode to your WordPress website, including its parameters, examples, and PHP function code. Additionally, we’ll assist you with common issues that might cause the Twitter Plugin shortcode not to show or not to work correctly.
Before starting, here is an overview of the Twitter Plugin and the shortcodes it provides:
"Twitter plugin is a user-friendly tool that integrates your WordPress site with your Twitter account, enabling seamless tweeting, retweeting and tracking Twitter engagement directly from your dashboard."
- [twitter_tracker]
Twitter [twitter_tracker] Shortcode
The Twitter Plugin shortcode is designed to handle attributes and content for the Twitter WordPress plugin. The shortcode, named ‘twitter_tracker’, uses a function ‘shortcodeHandler’ to process attributes and set options. If the ‘id’ option is not set, it returns an empty string. It then enqueues the Twitter WordPress JavaScript Loaders Tracking. The shortcode also ensures the tracking script is executed in the WordPress footer, providing efficient loading.
Shortcode: [twitter_tracker]
Parameters
Here is a list of all possible SHORTCODE_TAG shortcode parameters and attributes:
id
– A unique identifier for the Twitter tracking widget.attributes
– Array of attributes passed to the shortcode.content
– Optional content enclosed within shortcode tags.
Examples and Usage
Basic example – A simple usage of the Twitter plugin shortcode to initialize a tracker by referencing its ID.
[twitter_tracker id=123 /]
Advanced examples
Embedding a Twitter tracker with a specific ID and loading JavaScript asynchronously. The tracker will load after all footer scripts have been executed, improving page load speed.
[twitter_tracker id=123 async=true /]
Using the shortcode to initialize multiple Twitter trackers by passing their IDs as a comma-separated list. This allows for multiple tracking operations to be performed simultaneously.
[twitter_tracker id=123,456,789 /]
PHP Function Code
In case you have difficulties debugging what causing issues with [SHORTCODE_TAG]
shortcode, check below the related PHP functions code.
Shortcode line:
add_shortcode( static::SHORTCODE_TAG, array( $class, 'shortcodeHandler' ) );
Shortcode PHP function:
function shortcodeHandler( $attributes, $content = '' )
{
$options = static::getShortcodeAttributes( $attributes );
if ( ! isset( $options['id'] ) ) {
return '';
}
\Twitter\WordPress\JavaScriptLoaders\Tracking::enqueue();
static::$tracking_ids[ $options['id'] ] = true;
$class = get_called_class();
if ( false === has_action( 'wp_footer', array( $class, 'trackerJavaScript' ) ) ) {
// execute script after wp_print_footer_scripts action completes at priority 20
// async function queue should be inserted with footer scripts
add_action( 'wp_footer', array( $class, 'trackerJavaScript' ), 25 );
}
// execute all trackers just before </body>
return '';
}
Code file location:
twitter/twitter/src/Twitter/WordPress/Shortcodes/Advertising/Tracking.php
Conclusion
Now that you’ve learned how to embed the Twitter Plugin shortcode, understood the parameters, and seen code examples, it’s easy to use and debug any issue that might cause it to ‘not work’. If you still have difficulties with it, don’t hesitate to leave a comment below.
Leave a Reply