Shortcodes
A PHP or HTML snippet can run as a shortcode, so you decide exactly where its output appears: in a post, a page, a widget, or a page builder's shortcode element.
Make a snippet a shortcode
- Open the snippet and set How it runs to As a shortcode.
- Optionally type your own tag in Shortcode, for example
promo_banner. - Switch it on (Active) and save.
-
Place the shortcode in your content:
[snipfire id="12"]or, with your own tag:
[promo_banner]
[snipfire id="…"] always works, with or without a custom tag. The snippet's ID is shown in the sidebar and in the editor.
Using attributes
Attributes you add to the shortcode reach your code in the $atts array. Text between an opening and closing tag arrives in $content.
[promo_banner text="Summer sale" colour="#e63946"]
$text = $atts['text'] ?? 'Sale';
$colour = $atts['colour'] ?? '#000';
?>
<div class="promo" style="background: <?php echo esc_attr( $colour ); ?>">
<?php echo esc_html( $text ); ?>
</div>
That's an HTML snippet with PHP mixed in. A PHP snippet works the same way: whatever it prints (or returns as a string) becomes the shortcode's output.
With [snipfire id="12"], $atts also contains id.
Always escape attribute values before printing them (esc_html(), esc_attr(), esc_url()): anyone who can edit a post can put any value in a shortcode.
What visitors see when a snippet doesn't run
- Switched off, or in safe mode: the shortcode prints nothing. Visitors never see a raw
[promo_banner]. - Conditions don't match: the shortcode prints nothing.
- An error: the snippet is switched off automatically (see What Snipfire checks) and the shortcode prints nothing.
Rules for custom tags
- Letters, numbers,
-and_. Snipfire lowercases the tag. - Each tag can belong to only one snippet. If another snippet already uses it, Snipfire tells you when you save.
snipfireitself is reserved.
Shortcodes from other plugins
If you moved from another snippet plugin, its shortcodes keep working once you've switched the snippets over: [wpcode id="…"], [code_snippet id="…"], [fluent_snippet id="…"], [hfcm id="…"], Woody's [wbcr_php_snippet id="…"] (and its other wbcr_… shortcodes) and Post Snippets' own shortcode names run the imported snippet, even after you delete the old plugin. See Switching from another plugin.
Find where shortcodes are used
Snipfire → Tools & Safe Mode → Shortcodes in your content shows where snippet shortcodes are used on your site, and whether each one still prints something. Click Find shortcodes.
Snipfire looks in posts, pages and other post types (including block templates, template parts and patterns) and in widgets. It finds its own shortcodes, your custom tags, and the ones left over from WPCode, Code Snippets, FluentSnippets, Header Footer Code Manager, Woody Code Snippets and Post Snippets. For each one you see where it is (with a link to edit it), the shortcode, the Snipfire snippet it runs, and its state:
| State | Meaning |
|---|---|
| Works | A Snipfire shortcode whose snippet is on |
| Snippet switched off: prints nothing | The snippet exists but is off (or doesn't run as a shortcode) |
| Snippet deleted: prints nothing | No snippet with that ID any more |
| Old shortcode, can be rewritten | Another plugin's shortcode whose snippet you switched over to Snipfire |
| Old shortcode with its own options: keeps working as it is | A Code Snippets shortcode with format or shortcodes options, which change its output. It's left alone and keeps working |
| Served by the other plugin | Another plugin's shortcode whose snippet you haven't switched over |
A check covers up to 1,000 posts that contain snippet shortcodes. If there are more, the screen tells you.
Rewrite old shortcodes
Old shortcodes keep working without any change. But if you'd rather have [snipfire id="…"] everywhere, for example before removing the old plugin for good, click Rewrite … old shortcodes.
- Only shortcodes of snippets you switched over are rewritten. Attributes are kept:
[wpcode id="5" color="red"]becomes[snipfire id="12" color="red"]. - Each changed post gets a revision first (if its post type keeps revisions), so you can go back.
- Widgets are rewritten too.
- A rewrite handles up to 1,000 posts. On a bigger site, run it again until nothing is left to rewrite.
From the command line:
wp snipfire shortcodes # list every snippet shortcode found
wp snipfire shortcodes --state=missing # only the ones whose snippet was deleted
wp snipfire shortcodes --rewrite --dry-run # what a rewrite would change
wp snipfire shortcodes --rewrite # rewrite old shortcodes of switched-over snippets
States for --state: ok, rewrite, kept, other, missing, inactive. See WP-CLI.
Something unclear or missing? Tell us