Shortcodes are very popular in WordPress plugins. Today, we’ll be creating WordPress shortcodes. It’s relatively simple.
Create a new plugin called wpshortcodeex and update the index.php to look like this.
* Plugin Name: WP Shortcodes Example
* Plugin URI: http://jaskokoyn.com
* Description: A Basic WordPress Plugin to show how shortcodes work.
* Author: Jasko Koyn
* Version: 1.0
* Author URI: http://jaskokoyn.com
// Set up our WordPress Plugin
if ( version_compare( get_bloginfo( 'version' ), '3.1', '<' ) )
wp_die( "You must update WordPress to use this plugin!" );
register_activation_hook( __FILE__, 'jk_WPSCEX_check_WP_ver' );
// Include or Require any files
// Action & Filter Hooks
You’ll notice I renamed the function for checking the WordPress version. Reason is we don’t want this plugin to collide with any other functions. So, we give it a unique name. We add WPSCSEX which is short for WordPress shortcode example.
Creating WordPress Shortcodes
Under our action & filter hooks comment, let’s add the following code.
add_shortcode( 'jk', 'jk_WPSCEX_add_message' );
We also include our file shortcodes.inc.php file under the includes section
include( 'inc/shortcodes.inc.php' );
This is what our shortcodes.inc.php file looks like.
function jk_WPSCEX_add_message( $atts )
$output = '<a href="http://facebook.com/jaskokoyn">Like me on Facebook</a>';
Our jk_WPSCEX_add_message() function takes in 1 parameter, $atts. We then return a message saying to like me on Facebook. Activate your plugin and then create a new post and anywhere you like in the post and place the following code.
Publish your post and then look at it. You should see the message.
What’s going on?
The add_shortcode() function allows us to add shortcodes. It takes in 2 arguments, the first is the name of your shortcode. This is what the user would type in the square brackets in order to use the shortcode. The next parameter is the name of the function you want to run when someone uses your shortcode.
Our function takes in an argument as well. Which we called $atts, short for attributes. You can actually send data to our function to further extend the functionality of our plugin. We’re not going to talk about this in our current lesson, but the next one. So, don’t worry about it for now if you don’t understand the use for it. For now, just know when it comes to creating WordPress shortcodes, you can extend it with attributes.
Inside our function, we create a variable, $output, that contains our message and then we return that message. . Whenever someone types [jk] or whatever the name of your shortcode is, WordPress will automatically detect it and check if there’s a shortcode function for it. If it does find a function, then it will run that function and will exchange [jk] with whatever is returned from the function.
Do you see how useful shortcodes can be? Anyone who uses your plugin will be able to place the shortcode anywhere they want in their post. They also have the option whether or not they want to user your shortcode. Creating WordPress shortcodes can further be extended by allowing your users to define their own variables and messages. This is just a taste of the WordPress shortcode API. If you would like more info, then please go here.