Where snippets run
The Where setting chooses the point in the page load where a snippet runs. The list depends on the snippet type. Priority orders snippets (and other plugins' code) on the same location: Lower runs first. Default 10.
PHP
| Where | When it runs | Page conditions work? |
|---|---|---|
| Run immediately (like functions.php) (default) | As soon as Snipfire loads, while plugins are loading | No |
plugins_loaded |
After all plugins have loaded | No |
after_setup_theme |
After the theme has loaded | No |
init |
WordPress is set up; the usual place for registering post types | No |
wp_loaded |
WordPress, plugins and theme are fully loaded | No |
admin_init (admin only) |
At the start of every admin page | No |
wp (front end, query ready) |
On the front end, once WordPress knows which page it's showing | Yes |
template_redirect |
Just before the page template loads; good for redirects | Yes |
wp_head |
Inside <head> |
Yes |
wp_body_open |
Right after <body> opens (if the theme supports it) |
Yes |
wp_footer |
Before </body> |
Yes |
admin_head |
Inside <head> on admin pages |
No |
admin_footer |
At the bottom of admin pages | No |
| Custom hook… | When the action you name fires | Depends on the hook |
Most PHP snippets should use Run immediately. If your code calls add_action() or add_filter(), WordPress already runs the callback at the right time. Choose another location when the code itself needs to run at a certain point, for example code that reads the current page on template_redirect.
"Page conditions" means rules such as Page type, Post type or Post / page ID, which need WordPress to know which page it's showing. Rules about the user (Visitor is logged in, User role) work from plugins_loaded onwards. See Conditions and timing.
HTML
| Where | Where the HTML appears |
|---|---|
| Site header (wp_head) | Inside <head>: meta tags, tracking tags, preloads |
| After <body> opens (wp_body_open) | Right after the opening <body> tag, e.g. Google Tag Manager's <noscript> part |
| Site footer (wp_footer) (default) | Before </body> |
| Before post content | Above the content of posts and pages |
| After post content | Below the content of posts and pages |
| Before a paragraph of the post | Before paragraph N of the content |
| After a paragraph of the post | After paragraph N of the content |
| Before the excerpt (archives) | Above each excerpt on blog, archive and search pages |
| After the excerpt (archives) | Below each excerpt on blog, archive and search pages |
| Before a post in archive lists | Before post N in the list of posts |
| After a post in archive lists | After post N in the list of posts |
| Between posts in archive lists | Between every two posts in the list |
| Admin header | Inside <head> on admin pages |
| Admin footer | At the bottom of admin pages |
| Login page header | Inside <head> on the login page |
| Login page footer | At the bottom of the login page |
| Custom hook… | Wherever the action you name fires |
Before post content and After post content add the HTML to post content shown by the page's main loop: on a single post or page that's its content; on a blog or archive page that shows full posts, it's added to each post. Excerpts, widgets and secondary loops are left alone. To limit it to single posts and pages, add the condition Page type is Any single post/page.
After <body> opens relies on the theme calling wp_body_open(). Almost all current themes do.
Inside the post: before or after a paragraph
Use these for an ad, a newsletter box or a call to action in the middle of an article. Pick Before a paragraph of the post or After a paragraph of the post, then type the number in Paragraph number (on the classic edit screen, the field is called Position). For example, After a paragraph of the post with Paragraph number 3 puts the HTML after the third paragraph.
- Paragraphs are counted as they appear in the post's HTML, so a paragraph inside a quote or a column counts too.
- If a post has fewer paragraphs than the number, the HTML goes at the end of the content, so it's never lost.
- Like Before post content, it applies to post content shown by the page's main loop. Add Page type is Any single post/page to keep it off blog pages that show full posts.
On blog and archive pages: excerpts and post lists
These put HTML on pages that list posts: the blog, category and tag archives, search results. They work with block themes and with classic themes.
- Before the excerpt (archives) and After the excerpt (archives) add the HTML to each excerpt in the list. In a block theme, that's every Post Excerpt block on a page that isn't a single post or page. In a classic theme, it's each excerpt the theme prints in the main list. If your theme shows full posts instead of excerpts, use the post list locations instead.
- Before a post in archive lists and After a post in archive lists add the HTML once, next to post N. Type the number in Post number in the list.
1is the first post. The count starts again on each page of the list (page 2 starts at 1). If the page has fewer posts than the number, nothing is added. - Between posts in archive lists adds the HTML between every two posts, never before the first or after the last.
In a block theme, Snipfire adds the HTML as an extra item in the Query Loop block that shows the page's own posts (the one set to use the template's query, as archive templates do). The item has the class snipfire-insert, so you can style it with a CSS snippet. Other Query Loop blocks, such as a "latest posts" section on a page, are left alone.
In a classic theme, Snipfire prints the HTML as the theme starts the next post in its main list. Where that lands depends on the theme's markup, so check it on your site.
From the command line and the API
The number is the snippet's position field (1 or more). Set it with --position in WP-CLI:
wp snipfire create --title="Newsletter box" --type=html --hook=the_content:paragraph_after --position=3 --file=box.html
The location keys are in Location keys.
CSS
| Where | Loads on |
|---|---|
| Front end (default) | The public site |
| Front end and block editor | The public site and inside the block editor, so the editor looks like the site |
| Block editor only | The block editor, in the admin |
| Admin area | Admin pages |
| Login page | The login page |
JavaScript
| Where | Loads on |
|---|---|
| Front end, footer (default) | The public site, before </body> |
| Front end, header | The public site, in <head> |
| Admin area, footer | Admin pages, at the bottom |
| Admin area, header | Admin pages, in <head> |
| Login page | The login page |
CSS and JavaScript can't use custom hooks: they're added through WordPress's normal script and style loading. How they're printed is covered in Loading CSS and JavaScript.
Custom hooks (PHP and HTML)
Choose Custom hook… and type an action name in Custom hook name, for example woocommerce_before_add_to_cart_button. The snippet runs when that action fires, at the chosen priority:
- A PHP snippet's code runs the first time the action fires in a request (once per page load, even if the action fires again).
- An HTML snippet prints its HTML every time the action fires, at that point in the page.
Not sure which hook you need? The editor autocompletes WordPress's hooks, and with Snipfire Pro the visual hook finder shows every hook on a real page and creates the snippet for you.
Custom hooks are for actions. If you name a filter, your code still runs, but the filtered value passes through unchanged. To change a filtered value, write a PHP snippet that calls add_filter() and leave it on Run immediately:
add_filter( 'woocommerce_product_add_to_cart_text', function ( $text ) {
return 'Add to basket';
} );
Priority
When several snippets (or plugins) use the same location, lower priority numbers run first. The default is 10. For example, WooCommerce prints a product's title at priority 5 and its price at 10 on woocommerce_single_product_summary, so an HTML snippet on that hook at priority 7 appears between them.
When a location and a condition don't fit together
Some conditions can't be checked early in the page load. A Page type rule, for example, can't work on Run immediately, because WordPress doesn't know the page yet. Snipfire tells you when you save, and keeps the snippet switched off until you choose a later location or change the rule. On a custom hook Snipfire can't know the timing in advance, so a rule that can't be checked yet simply doesn't match.
Something unclear or missing? Tell us