
WordPress Plugin Development Cookbook
By :

Simple shortcodes already provide a lot of potential to output complex content to a page by entering a few characters in the post or page editors. That being said, they become even more useful when they are coupled with parameters that will be passed to their associated processing function. Using this technique, it becomes very easy to create a shortcode that accelerates the insertion of external content in WordPress posts or pages by only needing to specify the shortcode and the unique identifier of the source element to be displayed.
Follow these steps to create a shortcode that will be used to quickly add Twitter feeds to posts or pages:
plugins
directory of your development installation.ch2-twitter-embed
.ch2-twitter-embed.php
.Chapter 2 - Twitter Embed
.add_shortcode( 'twitterfeed', 'ch2te_twitter_embed_shortcode' );
ch2te_twitter_embed_shortcode
function:function ch2te_twitter_embed_shortcode( $atts ) { extract( shortcode_atts( array( 'user_name' => 'ylefebvre' ), $atts ) ); if ( empty( $user_name ) ) { $user_name = 'ylefebvre'; } else { $user_name = sanitize_text_field( $user_name ); } $output = '<p><a class="twitter-timeline" href="'; $output .= esc_url( 'https://twitter.com/'.$user_name ); $output .= '">Tweets by ' . esc_html( $user_name ); $output .= '</a></p><script async '; $output .= 'src="//platform.twitter.com/widgets.js"'; $output .= 'charset="utf-8"></script>'; return $output; }
[twitterfeed user_name='WordPress']
shortcode in the page editor, where WordPress
is the Twitter username of the feed to display:
Figure 2.11 – Inserting the Twitter feed shortcode with its user_name parameter
user_name
parameter and its associated value, only leaving the core [twitterfeed]
shortcode in the post; then Update to save changes.When shortcodes are used with parameters, these extra pieces of data are sent to the associated processing function in the $atts
parameter variable. By using a combination of the standard PHP extract
and WordPress-specific shortcode_atts
functions, our plugin is able to parse the data it receives and create an array of identifiers and values that are subsequently transformed into PHP variables. These variables are used in the rest of our shortcode implementation function. In this specific example, we expect a single variable to be used, called user_name
, which will be stored in a PHP variable called $user_name
. If the user enters the shortcode without any parameter, a default value of ylefebvre
will be assigned to the $user_name
variable to ensure that the plugin still works. Since we are going to accept user input in this code, we also verify that the user did not provide an empty string. We also use the sanitize_text_field
function to make sure that there is no hazardous code in what the user entered, along with the esc_html
and esc_url
functions to be absolutely sure to remove any potentially harmful HTML characters before we output the destination link and its accompanying text to the browser.
Once we have access to the Twitter username, we can put together the required HTML code that will embed a Twitter feed in our page and display the selected user's tweets.
While this example only has one argument, it is possible to define multiple parameters for a shortcode. In such a situation, parameters can be provided in any order by the user.