Master the WordPress Shortcode API to allow users to embed your plugin's content anywhere. Learn to register callbacks, handle attributes, and return HTML.
Previously in this course, we explored creating frontend templates to display our Knowledge Base articles. Now, we'll add a powerful layer of flexibility: the ability for users to embed this content anywhere on their site using a simple [shortcode].
A shortcode is a macro code—a small piece of text wrapped in square brackets—that WordPress replaces with dynamic content during the rendering process. For our Knowledge Base plugin, this means a user could type [kb_articles limit="5"] into any post or page, and our plugin would automatically inject the relevant list of articles.
At its core, the Shortcode API consists of three parts:
kb_articles) exists.limit="5").We need to register our shortcode during the init hook to ensure it's available globally. We’ll place this logic within our ShortcodeController.
PHP#6A9955">// In your ShortcodeController.php public function register() { add_shortcode('kb_articles', [$this, 'render_kb_articles']); } public function render_kb_articles($atts) { #6A9955">// Logic goes here return '<p>Knowledge Base content would go here.</p>'; }
Crucial rule: A shortcode callback must never echo content directly. If you echo inside the function, the content will appear at the very top of the page, regardless of where the user placed the shortcode. You must always return the generated HTML as a string.
Attributes allow users to customize the output. WordPress provides the shortcode_atts() function, which merges user-provided attributes with defaults.
Let's update our callback to accept a limit attribute:
PHPpublic function render_kb_articles($atts) { #6A9955">// Define defaults and merge with user input $args = shortcode_atts([ 'limit' => 3, 'category' => '' ], $atts, 'kb_articles'); #6A9955">// Use these attributes in your WP_Query arguments $limit = intval($args['limit']); #6A9955">// Example: Fetching articles using our Model layer #6A9955">// $articles = $this->model->get_recent_articles($limit); return "<p>Displaying {$limit} articles.</p>"; }
The third parameter in shortcode_atts is the shortcode name itself; this allows other developers to filter your default attributes using the shortcode_atts_{$shortcode} hook, making your plugin highly extensible.
includes/Controllers/ShortcodeController.php in your plugin.register() method and add a render_kb_articles() method.ShortcodeController and call register() during the init action.[kb_articles limit="10"] to a WordPress post and verify the output on the frontend.echo breaks the document flow. Use an output buffer (ob_start() and ob_get_clean()) if you need to include complex template files within your shortcode.$atts array before passing it to database queries.By mastering shortcodes, you've bridged the gap between static content and dynamic, user-controlled embedding. We've learned to register the tag, define a callback that returns content, and use shortcode_atts to make our plugin flexible. These skills are essential for any professional WordPress developer, especially when creating custom post types that need to be surfaced in various parts of a site.
Up next: We'll dive into Advanced Shortcode Logic, where we will use output buffering and conditional rendering to create even more complex layouts.
Learn to extend the WordPress REST API by registering custom endpoints. We'll show you how to securely serve your Knowledge Base data as structured JSON.
Read moreMaster advanced shortcode logic in WordPress. Learn to use shortcode_atts, implement conditional rendering, and sanitize output for robust, secure plugin embeds.
Building Shortcodes
Plugin Security Best Practices
Composer for Dependencies
Theme Integration Hooks
Managing Assets with Gulp/Webpack
Documentation Standards
Plugin Deployment Strategy
Advanced MVC: Dependency Injection
Handling Large Datasets
Error Handling and Logging