Master advanced shortcode logic in WordPress. Learn to use shortcode_atts, implement conditional rendering, and sanitize output for robust, secure plugin embeds.
Previously in this course, we covered the basics of Building Shortcodes. While you now know how to register a simple tag, production-ready plugins require more than just static output. To build a professional-grade Knowledge Base, your shortcodes must be dynamic, attribute-driven, and—above all—secure.
In this lesson, we are evolving our Knowledge Base shortcode to handle user-defined attributes and conditional logic.
Shortcodes are most useful when they behave like functions: they accept arguments (attributes) and return a specific output based on those inputs. Without attributes, a shortcode is just a static string. With them, you can create a single [kb_article] tag that displays different content based on an ID, a category, or a display style.
To handle these attributes safely, WordPress provides the shortcode_atts() function. This function merges user-provided attributes with a set of default values, ensuring that your code always has a fallback if a user forgets to specify a parameter.
Let’s enhance our Knowledge Base plugin. We want a shortcode that accepts an id attribute to fetch a specific article and a style attribute to toggle between a "simple" or "card" layout.
PHPfunction render_kb_article_shortcode($atts) { #6A9955">// 1. Define defaults and merge with user attributes $args = shortcode_atts( array( 'id' => 0, 'style' => 'simple', ), $atts, 'kb_article' ); #6A9955">// 2. Conditional logic based on attributes $article_id = intval($args['id']); if ($article_id === 0) { return '<p>Please provide a valid article ID.</p>'; } #6A9955">// Fetch data using our Model(assuming access to your KnowledgeBaseModel) $article = KnowledgeBaseModel::get_article($article_id); if (!$article) { return '<p>Article not found.</p>'; } #6A9955">// 3. Conditional rendering based on style ob_start(); if ($args['style'] === 'card') { include 'views/kb-card-template.php'; } else { include 'views/kb-simple-template.php'; } #6A9955">// 4. Proper output sanitization return wp_kses_post(ob_get_clean()); } add_shortcode('kb_article', 'render_kb_article_shortcode');
In the example above, we used wp_kses_post(). This is non-negotiable. Shortcodes are often placed in the post editor by users who might not have administrative privileges. If you echo raw HTML or database content directly, you open your site to Cross-Site Scripting (XSS) attacks.
Just as we discussed when Sanitizing User Input, you must treat all data—even data coming from your own database—as potentially malicious before it hits the frontend. Never trust the data source; always escape the output.
Modify your current Knowledge Base shortcode to include the following logic:
show_excerpt (default: true).show_excerpt is 'true'.'false', hide it.intval() to prevent injection attempts.echo inside your function, the content will appear at the top of the page, outside the intended post container.ID or Style in your shortcode_atts array; always use lowercase keys.$post inside your shortcode if possible. Pass necessary data through attributes or class properties to keep your code testable and predictable.ShortcodeController class. Keep your callback function lean; its only job should be to delegate to the controller.By mastering shortcode_atts, you’ve moved from static text replacement to creating a dynamic, attribute-driven interface for your users. We’ve enforced security through sanitization and leveraged conditional logic to make our Knowledge Base plugin more flexible. Remember: always define defaults, validate/cast inputs, and sanitize every piece of output that leaves your server.
Up next: We will begin our transition into the modern block editor by exploring Gutenberg Blocks and how to register them using PHP.
Master WordPress security by implementing capability checks. Learn to use current_user_can to restrict admin features and enforce proper access control.
Advanced Shortcode Logic
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