Wednesday, September 3, 2025
•3 min read
How to Create a Custom Shortcode in WordPress Without Plugins
Shortcodes are one of WordPress’s simplest but most powerful features. They let you add dynamic or reusable content anywhere — inside posts, pages, or widgets — without writing long chunks of PHP or HTML every time. Here’s how to create your own custom shortcodes, step by step, without relying on any plugins.
What Is a WordPress Shortcode?
A shortcode is a small snippet wrapped in square brackets, like `[gallery]` or `[caption]`, that runs a function behind the scenes to display something dynamic. You can drop shortcodes anywhere inside your content and WordPress will replace them with whatever the shortcode’s function returns.
Why Use Shortcodes?
Shortcodes make it easy to reuse content and functionality. You can use them for things like custom forms, pricing tables, buttons, or embeds. They help you keep your code modular and clean — one function that can be used in multiple places with just a few keystrokes.
Step 1: Define the Shortcode Handler Function
A shortcode works by calling a function whenever WordPress detects the shortcode tag. The function defines what will be output. For example:
function custom_shortcode_handler() {
return '<p>This is my custom shortcode output!</p>';
}Step 2: Register the Shortcode
Once your handler function is ready, you register it with WordPress using `add_shortcode()`. Add this code to your theme’s `functions.php` file or a custom plugin:
add_shortcode( 'custom_shortcode', 'custom_shortcode_handler' );Now you can use `[custom_shortcode]` anywhere in a post, page, or widget, and WordPress will output the content defined in your handler function.
Step 3: Add Attributes to Your Shortcode
Shortcode attributes let users modify the output. Here’s how to define a shortcode that accepts a color attribute:
1function colored_text_shortcode( $atts ) {
2 $atts = shortcode_atts( array(
3 'color' => 'black',
4 ), $atts, 'colored_text' );
5
6 return '<p style="color:' . esc_attr( $atts['color'] ) . ';">This is colored text!</p>';
7 }
8 add_shortcode( 'colored_text', 'colored_text_shortcode' );Now you can use `[colored_text color="red"]` to output red text, or change the color value as needed.
Step 4: Create Enclosing Shortcodes
Enclosing shortcodes let you wrap content between an opening and closing tag. This is useful for applying formatting or wrapping dynamic containers around text.
function wrap_shortcode( $atts, $content = null ) {
return '<div class="wrap">' . do_shortcode( $content ) . '</div>';
}
add_shortcode( 'wrap', 'wrap_shortcode' );You can now use `[wrap]Your content here[/wrap]` to wrap custom markup around any content.
Step 5: Use Shortcodes Inside Widgets
By default, WordPress doesn’t process shortcodes in widgets. To enable that, add this filter to your functions file:
add_filter( 'widget_text', 'do_shortcode' );This allows any shortcode to render correctly inside text widgets. You can now go to **Appearance → Widgets**, drag in a Text widget, and add `[custom_shortcode]` or any other shortcode you’ve created.
Default WordPress Shortcodes
WordPress ships with several built-in shortcodes like `[gallery]`, `[caption]`, and `[embed]`. These let you insert image galleries, captions, or embedded media without any custom code.
Real-World Examples
- Create a shortcode that displays your business’s opening hours across multiple pages. This keeps your hours consistent everywhere, and any changes can be made from one place in your theme or settings.
- Build a shortcode that displays a pricing table for your services. You can define your prices in a custom post type or array and generate a dynamic table with a single shortcode line.
Useful Links
Official WordPress Shortcode API reference:
WordPress Developer Handbook – Shortcode API examples:
Shortcodes are a simple but powerful way to keep your WordPress site flexible. Once you understand how they work, you can build reusable components that save time and keep your templates clean — all without installing a single plugin.

I’m an experienced SEO professional with over a decade of helping over 100 businesses rank higher online, especially local businesses, e-commerce stores and SaaS. As the co-founder of LPagery, I specialize in practical, proven strategies for regular SEO and Local SEO success.