Skip to content
WORDPRESS, PHP & DATABASE

WordPress Shortcode Generator – PHP Code

Generate secure WordPress shortcode starter code.

  • Free
  • No signup
  • Runs in your browser

This tool runs in your browser. The text you enter is processed on your device and is not sent to NHP Learning.

WordPress Shortcode Generator

About WordPress Shortcode Generator

A shortcode is a short tag in square brackets, such as [notice], that WordPress replaces with dynamic HTML when a post or page is displayed. They let editors add boxes, buttons, forms or lists without writing code, and they work in the classic editor, the Shortcode block and many page builders.

Enter a shortcode tag name and the generator writes a complete starter function: it registers the tag with add_shortcode(), reads attributes with shortcode_atts(), supports enclosed content, and escapes output with esc_html() and wp_kses_post(). Paste the code into a small custom plugin or your child theme's functions.php.

How to use WordPress Shortcode Generator

  1. Type the shortcode tag, for example nhp_notice. Spaces and symbols become underscores.
  2. Click Run Tool to generate the PHP.
  3. Paste it into a custom plugin or a code snippets plugin, then use the tag in a post as [nhp_notice title="Hello"]Text[/nhp_notice].

Example

Input

nhp_notice

Output

// Usage: [nhp_notice title="Hello"]Your content[/nhp_notice]
function nhp_notice_shortcode( $atts, $content = null ) {
    $atts = shortcode_atts( array(
        'title' => '',
    ), $atts, 'nhp_notice' );

    ob_start();
    ?>
    <div class="nhp-notice">
        <?php if ( '' !== $atts['title'] ) : ?>
            <h3><?php echo esc_html( $atts['title'] ); ?></h3>
        <?php endif; ?>
        <?php echo wp_kses_post( do_shortcode( (string) $content ) ); ?>
    </div>
    <?php
    return ob_get_clean();
}
add_shortcode( 'nhp_notice', 'nhp_notice_shortcode' );

Common uses

  • Creating a reusable call-to-action box or notice that editors can insert anywhere.
  • Building a shortcode that outputs a list, a form or a data table from a custom query.
  • Learning the structure of a safe WordPress shortcode before adding your own logic.

Privacy and safety

This tool runs in your browser. The text you enter is processed on your device and is not sent to NHP Learning.

Frequently asked questions

Where should I put the code?

In a small custom plugin, or use a snippets plugin. Code placed in a theme's functions.php disappears when you change the theme.

Why does the generated code return output instead of echoing it?

Shortcodes must return their HTML. Echoing prints it at the wrong place in the page. The generated function buffers the HTML with ob_start() and returns it.

How do I add more attributes?

Add more keys to the array passed to shortcode_atts(), then use them as $atts['key'] and escape them when you output them.

Learn to build this yourself

Free tutorials and courses on NHP Learning cover PHP, JavaScript, SQL and WordPress, from the basics to real projects.