- Navigate to the WordPress plugin directory of your development installation.
- Create a new directory called ch2-twitter-shortcode.
- Navigate to this directory and create a new text file called ch2-twitter-shortcode.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 Shortcode.
- Add the following line of code to declare a new shortcode, simply using the two characters tl, and specify the name of the function that should be called when the code is encountered in posts or pages:
add_shortcode( 'tl', 'ch2ts_twitter_link_shortcode' );
- Add the following code section to provide an implementation for the ch2ts_twitter_link_shortcode function:
function ch2ts_twitter_link_shortcode( $atts ) {
$output = '<a href="https://twitter.com/ylefebvre">';
$output .= 'Twitter Feed</a>';
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.
- Edit an existing post on your site and use the shortcode [tl] in the code editor:
- Save and view the post to see that the shortcode was replaced by a link to a Twitter page attached to the words Twitter Feed.