- Navigate to the WordPress plugin directory of your development installation.
- Create a new directory called ch2-twitter-embed.
- Navigate to this directory and create a new text file called ch2-twitter-embed.php.
- Open the new file in a code editor and add an appropriate header at the top of the plugin file, naming the plugin Chapter 2 - Twitter Embed.
- Add the following line of code to declare a new shortcode and specify the name of the function that should be called when the shortcode is found in posts or pages:
add_shortcode( 'twitterfeed', 'ch2te_twitter_embed_shortcode' );
- Add the following code section to provide an implementation for the ch2te_twitter_embed_shortcode function:
function ch2te_twitter_embed_shortcode( $atts ) {
extract( shortcode_atts( array(
'user_name' => 'ylefebvre'
), $atts ) );
if ( !empty( $user_name ) ) {
$output = '<a class="twitter-timeline" href="';
$output .= esc_url( 'https://twitter.com/' . $user_name );
$output .= '">Tweets by ' . esc_html( $user_name );
$output .= '</a><script async ';
$output .= 'src="//platform.twitter.com/widgets.js"';
$output .= ' charset="utf-8"></script>';
} else {
$output = '';
}
return $output;
}
- Save and close the plugin file.
- Log in to the administration page of your development WordPress installation.
- Click on Plugins in the left-hand navigation menu.
- Activate your new plugin.
- Create a new page and use the shortcode [twitterfeed user_name='WordPress'] in the page editor, where WordPress is the Twitter username of the feed to display:
- Publish and view the page to see that the shortcode has been replaced by an embedded Twitter feed on your site.
- Edit the page and remove the user_name parameter and its associated value, only leaving the core [twitterfeed] shortcode in the post, then Update to save changes.
- Refresh the page and see that the feed is still being displayed, but now shows tweets from another account.