O'Reilly logo

WordPress® All-in-One For Dummies® by Michael Torbert, Andrea Rennick, Kevin Palmer, Cory Miller, Lisa Sabin-Wilson

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Using Custom Shortcodes

One of the most common inefficiencies with plugins is when a plugin wants to add information within the body of a post or page. The plugin developer manually creates a bloated filtering function, hooks into the_content (the function tag that calls the body of the content from the database and delivers it to your web site), and filters it in an attempt to find the appropriate spot to display the information. Fortunately, WordPress has a built-in solution for this. Using the shortcode API, your users can easily choose where in a given post to display the information your plugin is providing them.

The basic premise is that you have a string of data that your plugin dynamically generates, and you want your users to determine where in each post and page it displays. From your users' perspective, they will type a shortcode like this within the body of their content in order to display information from a plugin:

[myshortcode]

On the developer side, you just use the add_shortcode function and add it to your primary plugin PHP file:

<?php add_shortcode($tag, $func); ?>

The add_shortcode function accepts two parameters: The $tag parameter is the string that users will type within the body of their content to make a call to the plugin shortcode (from the previous example: [myshortcode] is what the users type, so your $tag parameter would be: myshortcode. The $func parameter is your callback function (a function that you still need to define with in the body of ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required