Create a WordPress shortcode with attributes the right way
A shortcode is a tag in square brackets, like [my_shortcode], that WordPress replaces with the output of a PHP function when the content is displayed. Attributes let editors change the output without code: [button label="Click me" color="blue" url="https://example.com"]. A shortcode can be self-closing or enclosing, wrapping some content: [highlight]Important text[/highlight].
Problem: Let editors insert a styled button or a highlighted text into posts with a simple tag and a few options, without writing HTML.
Solution: Register the shortcode with add_shortcode(), merge the attributes with defaults using shortcode_atts(), check every value against an allowlist or escape it, and return the HTML.
A self-closing shortcode
<?php
// [helloadmin_button label="Go" url="https://..." color="blue" size="md"]
add_shortcode( 'helloadmin_button', 'helloadmin_button_shortcode' );
function helloadmin_button_shortcode( $atts ) {
$atts = shortcode_atts( array(
'label' => 'Click here',
'url' => '#',
'color' => 'blue', // blue | green | red
'size' => 'md', // sm | md | lg
), $atts, 'helloadmin_button' );
$color = in_array( $atts['color'], array( 'blue', 'green', 'red' ), true ) ? $atts['color'] : 'blue';
$size = in_array( $atts['size'], array( 'sm', 'md', 'lg' ), true ) ? $atts['size'] : 'md';
return sprintf(
'<a href="%s" class="btn btn-%s btn-%s">%s</a>',
esc_url( $atts['url'] ),
esc_attr( $color ),
esc_attr( $size ),
esc_html( $atts['label'] )
);
}
shortcode_atts() drops attributes you did not declare and fills in the missing ones. Its third argument, the shortcode name, enables the shortcode_atts_helloadmin_button filter, so other code can change the defaults.
An enclosing shortcode
The enclosed content comes as the second argument:
<?php
// [helloadmin_highlight color="yellow"]Important text[/helloadmin_highlight]
add_shortcode( 'helloadmin_highlight', 'helloadmin_highlight_shortcode' );
function helloadmin_highlight_shortcode( $atts, $content = '' ) {
$atts = shortcode_atts( array( 'color' => 'yellow' ), $atts, 'helloadmin_highlight' );
$color = in_array( $atts['color'], array( 'yellow', 'green', 'pink' ), true ) ? $atts['color'] : 'yellow';
return '<mark class="highlight highlight--' . esc_attr( $color ) . '">' . do_shortcode( wp_kses_post( $content ) ) . '</mark>';
}
The color is a class from a fixed list, not an inline style: any user who can write posts can type attributes, and a free value in style="..." would let them inject arbitrary CSS. do_shortcode() on the content allows nested shortcodes.
Rules
- Return, do not echo. Shortcodes run while the content is being built, so echoed output appears before the content, at the top of the post. If the HTML is long, collect it with
ob_start()andreturn ob_get_clean();. - Escape everything:
esc_url()for URLs,esc_attr()in attributes,esc_html()for text. - Put shortcodes in a plugin if the content depends on them: with functions.php they disappear on a theme change, and the posts show raw
[tags]. - If the shortcode needs a script, enqueue it from the callback with
wp_enqueue_script(): it will be printed in the footer only on pages that use the shortcode (see enqueueing scripts and styles).
NOTE: In the block editor, shortcodes work in the Shortcode block and inside paragraphs. The Text widget also runs shortcodes since WordPress 4.9, so add_filter( 'widget_text', 'do_shortcode' ) is no longer needed.