WordPress shortcodes are one of the platform’s most useful features, allowing you to add complex functionality to your content with simple text commands. Whether you’re embedding a contact form, displaying a gallery, or adding custom elements, shortcodes make it possible without touching a line of code.

This comprehensive guide will walk you through everything you need to know about WordPress shortcodes—from understanding the basics to creating your own custom solutions.
Table of Contents
- What is a Shortcode in WordPress?
- Types of WordPress Shortcodes
- Exploring Default WordPress Shortcodes
- Practical Use Cases and Benefits
- How to Insert Shortcodes in WordPress
- How to Create Custom Shortcodes
- Plugins for Shortcodes
- Shortcodes vs. Gutenberg Blocks
- Best Practices and Maintenance
What is a Shortcode in WordPress?
A shortcode in WordPress is a small piece of code wrapped in square brackets, like
Origin and Purpose
WordPress introduced shortcodes in version 2.5 as a solution to a common problem: users wanted to embed complex elements like galleries, forms, and multimedia content, but doing so required technical knowledge that many didn’t have. Shortcodes simplified this process by creating a standardized way to add functionality through simple text commands.
The concept draws inspiration from BBCode, which was popular in forum software. However, WordPress shortcodes are more powerful because they can execute PHP functions and interact with the WordPress database.
Examples in Use
Here are some common shortcodes you might recognize:
Displays an image gallery:
[gallery ids="1,2,3"]
Show a contact form:
[wpzf_form id="807"]
Embed a video player:
[video src="movie.mp4"]
When WordPress processes your content, it automatically converts these shortcodes into the corresponding HTML and functionality.
Types of WordPress Shortcodes
WordPress supports two main types of shortcodes, each serving different purposes and offering varying levels of complexity.
Self-Closing Shortcodes
Self-closing shortcodes are standalone commands that don’t require closing tags. They’re similar to HTML’s self-closing elements, like <br /> or <img />. These shortcodes typically display specific content or trigger particular functionality.
Here is an example of a custom self-closing shortcode: [current-year] – Shows the current year.

Enclosing Shortcodes
Enclosing shortcodes work like HTML tags with opening and closing elements. They wrap around content and modify or enhance whatever appears between the tags. This type is useful when you want to apply formatting or functionality to specific text or content.
Examples of enclosing shortcodes:
- [highlight]Important text here[/highlight] – Highlights the enclosed text
- [button]Click me[/button] – Creates a button with the enclosed text
- [tooltip title=”More info”]Hover over this[/tooltip] – Adds a tooltip to text
Attributes and Parameters
Both types of shortcodes can accept attributes (also called parameters) that customize their behavior. Attributes work similarly to HTML attributes, allowing you to pass specific values to control how the shortcode functions.
Basic attribute syntax:
[shortcode attribute="value"]
Multiple attributes:
[gallery columns="3" size="medium" ids="1,2,3"]
Attributes make shortcodes incredibly flexible. For example, the same gallery shortcode can display different numbers of columns, image sizes, or specific images based on the attributes you provide.
Exploring Default WordPress Shortcodes
WordPress comes with several built-in shortcodes that handle common content needs. Understanding these default options can save you time and help you avoid installing unnecessary plugins.
Audio and Video
The and shortcodes provide easy ways to embed media files directly into your content. These shortcodes support multiple formats and offer various customization options.
Audio shortcode attributes:
- src – Direct link to audio file
- loop – Whether to repeat playback
- autoplay – Start playing automatically (use sparingly)
- preload – How much to buffer before playing
Example:
[audio src="podcast-episode.mp3" loop="off" preload="metadata"]
Video shortcode attributes:
- src – Direct link to video file
- width and height – Player dimensions
- poster – Thumbnail image shown before play
- controls – Show or hide player controls
Example:
[video width="640" height="360" poster="thumbnail.jpg" src="presentation.mp4"]
Gallery and Images
The gallery shortcode is one of WordPress’s most popular built-in features. It automatically creates image galleries from your media library with minimal effort.
Key gallery attributes:
- ids – Specific image IDs to include
- columns – Number of columns to display (default: 3)
- size – Image size (thumbnail, medium, large, full)
- orderby – How to sort images (date, title, rand)
Example:
[gallery columns="4" size="medium" ids="15,23,45,67"]
The caption shortcode adds styled captions to images and is automatically generated when you add captions through the media library interface.
Embed and Playlist
WordPress’s embed shortcode works with oEmbed providers like YouTube, Vimeo, Twitter, and Instagram. Simply paste a URL and WordPress handles the embedding automatically.
Example:
[embed width="560" height="315"]https://www.youtube.com/watch?v=example[/embed]
The playlist shortcode creates playlists from multiple audio or video files, perfect for showcasing collections of media content.
Playlist attributes:
- type – “audio” or “video”
- ids – Comma-separated list of media IDs
- style – “light” or “dark” theme
Other Built-Ins
WordPress includes a few other default shortcodes worth knowing about:
- wp_caption – Alternative caption formatting
- embed – Handles various external content embedding
⚡ Note: Some older shortcodes like [soundcloud] have been deprecated as WordPress moves toward more standardized embedding methods. Always check the latest WordPress documentation for current shortcode support.
Upgrade Your Website with a Premium WordPress Theme
Find a theme that you love and get a 10% discount at checkout with the FLASH10 code
Choose your theme
Practical Use Cases and Benefits
Shortcodes excel in specific scenarios where you need consistent, reusable functionality across your website. Here are the most common and effective applications.
Forms and Calls-to-Action
Contact forms are probably the most popular shortcode use case. Plugins like WPZOOM Forms generate shortcodes that you can place anywhere on your site.
Benefits include:
- Consistency – The same form appears identical wherever you place it
- Easy updates – Change the form once, and it updates everywhere
- No coding required – Anyone can insert forms into content
Newsletter signup forms, feedback forms, and survey embeds all work the same way. Once you create the form, the shortcode makes it available throughout your site.
Multimedia and Interactive Elements
Shortcodes shine when adding interactive elements that would otherwise require complex HTML or JavaScript. Common examples include:
- Image sliders and carousels – Display multiple images in rotating formats
- Testimonial showcases – Highlight customer reviews with consistent styling
- Tabbed content – Organize information into easy-to-navigate sections
- Accordion menus – Create expandable content sections
These elements improve user experience by making content more engaging while maintaining design consistency across your site.
E-commerce and Dynamic Data
For online stores and business websites, shortcodes can display dynamic information that updates automatically:
- Product showcases – Display featured items with current pricing
- Pricing tables – Compare services or subscription plans
- Current dates and times – Show hours of operation or event countdowns
- Custom calculators – Help visitors estimate costs or requirements
The key advantage is that this information stays current without manual updates to every page where it appears. For e-commerce functionality, WooCommerce offers extensive shortcode support for product displays and checkout processes.
SEO and Design Consistency
From an SEO perspective, shortcodes help maintain a consistent markup structure across your content. Search engines prefer websites with predictable, well-structured HTML, and shortcodes help ensure that similar content types always use the same formatting.
Design-wise, shortcodes prevent the common problem of content creators using inconsistent styling or markup when trying to achieve similar effects manually.
⚡ For more insights on WordPress SEO best practices, explore our comprehensive WordPress SEO guide.
How to Insert Shortcodes in WordPress
The method for adding shortcodes depends on which editor you’re using and where you want the shortcode to appear.
Gutenberg Shortcode Block
The WordPress Block Editor (Gutenberg) includes a dedicated Shortcode block specifically for this purpose. Here’s how to use it:
- Click the “+” button to add a new block
- Search for “Shortcode” or find it in the Widgets category
- Click the Shortcode block to add it
- Type or paste your shortcode into the text field
- The shortcode will render when you publish or preview the page

Real-world example: To add a WPZOOM contact form in the middle of a blog post, insert a Shortcode block and enter [wpzf_form id=”807″]. The form will appear exactly where you placed the block.
Classic Editor and Legacy Content
If you’re still using the Classic Editor or working with older content, inserting shortcodes is even simpler:
- Position your cursor where you want the shortcode output to appear
- Type the shortcode directly into the content area
- WordPress automatically processes shortcodes when displaying the content
This method also works when editing existing posts that contain shortcodes—you can modify the shortcode parameters directly in the text editor.
Widgets and Theme Files
Shortcodes work in WordPress widgets, making them useful for sidebar content, footer elements, and other widget areas. When adding shortcodes to widgets:
- Go to Appearance → Widgets in your WordPress admin
- Add a Text or Custom HTML widget to your desired widget area
- Enter your shortcode in the widget content area
For developers working with theme files, use the do_shortcode() function to process shortcodes in PHP templates:
echo do_shortcode('[your-shortcode-here]');
How to Create Custom Shortcodes
Creating your own shortcodes gives you complete control over functionality and appearance. The process involves writing a PHP function and registering it with WordPress.
Writing the Callback Function
Your shortcode callback function is what actually generates the output. The most important rule: always return the content, never echo it directly.
Here’s a basic example that displays the current year:
function display_current_year() {
return date('Y');
}
For more complex shortcodes, you might query the database, format HTML, or perform calculations before returning the result.
Registering with add_shortcode()
Once you’ve written your function, register it as a shortcode using add_shortcode(). Add this code to your theme’s functions.php file or in a custom plugin:
function display_current_year() {
return date('Y');
}
add_shortcode('current-year', 'display_current_year');
Now you can use [current-year] anywhere on your site, and it will display the current year.
⚡ Pro Tip: If you’re using a third-party theme, consider creating a child theme to prevent losing your customizations when the theme updates.
Adding Attributes with shortcode_atts()
Attributes make shortcodes much more flexible. Use shortcode_atts() to handle user-provided parameters safely:
function custom_button_shortcode($atts) {
$atts = shortcode_atts(array(
'text' => 'Click Here',
'url' => '#',
'color' => 'blue',
), $atts);
return '<a href="' . esc_url($atts['url']) . '" class="btn btn-' . esc_attr($atts['color']) . '">' . esc_html($atts['text']) . '</a>';
}
add_shortcode('button', 'custom_button_shortcode');
This creates a flexible button shortcode:
[button text="Contact Us" url="/contact" color="green"]
Embedding with do_shortcode()
Sometimes you need to execute shortcodes within PHP template files or widgets that don’t automatically process them. Use the do_shortcode() function:
// In a template file
echo do_shortcode('[gallery columns="3"]');
// Processing content that might contain shortcodes
$content = get_post_meta(123, 'custom_content', true);
echo do_shortcode($content);
Troubleshooting and Best Practices
Common issues when creating custom shortcodes:
- Namespace conflicts: Choose unique shortcode names to avoid conflicts with plugins. Prefix your shortcodes with your site name or theme name.
- Security considerations: Always sanitize user input using functions like esc_html(), esc_attr(), and esc_url(). Never trust user-provided data.
- Debug tip: If your shortcode isn’t working, check that you’re returning (not echoing) content and that you’ve properly registered it with add_shortcode().
- Screenshot suggestion: Display the functions.php file being edited with shortcode code, and show the corresponding shortcode working on the frontend.
Plugins for Shortcodes
While creating custom shortcodes gives you complete control, plugins can provide ready-made solutions for common needs.
- Shortcodes Ultimate is one of the most comprehensive shortcode collections available. It provides dozens of pre-built shortcodes for buttons, columns, tabs, accordions, and more. The plugin includes a visual shortcode generator that makes it easy to configure complex layouts without memorizing syntax.
- Shortcoder takes a different approach by letting you create and manage your own shortcodes through a visual interface. You can build custom shortcodes using HTML, CSS, and even JavaScript without touching your theme files.
Shortcodes vs. Gutenberg Blocks
Understanding when to use shortcodes versus Gutenberg blocks helps you choose the right tool for each situation.
Differences and Similarities
- Visual editing: Blocks provide immediate visual feedback in the editor, while shortcodes show only their code until you preview or publish the page.
- Flexibility: Shortcodes work anywhere WordPress processes content (posts, pages, widgets, even comments). Blocks are primarily designed for the main content editor.
- User experience: Blocks offer a more intuitive interface for non-technical users, with settings panels and drag-and-drop functionality.
When to Use Shortcodes
Shortcodes remain the better choice for:
- Legacy content: If you have extensive existing content with shortcodes, there’s no urgent need to convert everything to blocks.
- Widget areas: Shortcodes work seamlessly in sidebars and footer widgets, while blocks are limited to the main content area.
- Template integration: Developers can easily embed shortcodes in theme files using do_shortcode().
- Cross-platform content: If you syndicate content to multiple sites or platforms, shortcodes provide more portable functionality.
Migrating to Blocks
If you want to transition from shortcodes to blocks:
- Gradual approach: Start using blocks for new content while leaving existing shortcode content unchanged
- Plugin solutions: Some plugins offer block versions of their shortcodes
- Custom block development: Convert frequently-used custom shortcodes to custom blocks for a better user experience
The key is not to rush this transition—both shortcodes and blocks can coexist effectively.
Best Practices and Maintenance
Proper shortcode management ensures your site remains fast, secure, and maintainable over time.
- Limit shortcode quantity: Too many different shortcodes can overwhelm content creators and slow down your site. Focus on the ones you actually use regularly.
- Input validation: When creating custom shortcodes, always validate and sanitize user input. Use WordPress functions like sanitize_text_field() and wp_kses() to prevent security vulnerabilities.
- Performance considerations: Complex shortcodes that perform database queries can slow page loading. Consider caching mechanisms for resource-intensive shortcodes.
- Maintain a shortcode reference: Keep a list of all shortcodes used on your site, including their purposes and parameters. This helps when troubleshooting or training new team members.
- Track usage locations: Note where each shortcode appears so you can easily update or remove them if needed. This becomes especially important when dealing with security updates or plugin changes.
- Use staging environments: Test shortcode changes on a staging site before applying them to your live website.
- Stay informed: Keep up with WordPress updates and plugin changelogs. Shortcode syntax or functionality might change between versions.
- Regular audits: Periodically review your shortcodes to remove unused ones and ensure all active shortcodes still function correctly.
- Screenshot suggestion: Show a WordPress admin dashboard with a staging site setup and shortcode testing in progress.
Stop Struggling with Complex WordPress Customizations
Tired of wrestling with code just to add simple functionality to your site? WPZOOM themes come pre-built with extensive shortcode support and intuitive customization options that work seamlessly with both classic shortcodes and modern Gutenberg blocks.
Skip the technical headaches and focus on what matters most—creating content that connects with your audience. Our themes handle the technical complexity so you can concentrate on growing your business. Discover WPZOOM themes.
