WPCode Logo

WPCode Documentation

Documentation, Reference Materials, and Tutorials for WPCode

Most Popular

Get free tips and resources right in your inbox, along with 500+ others

Follow Us

A Complete Guide to Snippet Locations

Would you like a snippet to run only on single posts, only inside your WordPress dashboard, or only partway through an article? WPCode calls that choice the snippet’s location, and the paid version offers 65 of them. All you need is WPCode and a snippet to work with.

In this guide, we’ll walk you through picking a location for a snippet and cover every location WPCode provides.

Requirements: The Anywhere, Content, and eCommerce location groups come with the paid version of WPCode.

Before you get started, make sure WPCode is installed and activated on your WordPress site.

How Do Snippet Locations Work?

A location is the point in a page where WPCode drops your snippet. Once you set a location, the snippet runs there on its own every time that point is reached. That is what WPCode means by Auto Insert.

The alternative is Shortcode. In that mode, WPCode never places the snippet for you. It runs only where you paste its shortcode.

Each snippet holds a single location. It is either auto inserted there or driven by a shortcode, never both. If you need the same code in two places, add a second snippet.

WPCode sorts its locations into three categories, which you’ll see as a rail down the left side of the picker:

  • Global locations run on every page, or across a whole area of your site such as the dashboard.
  • Page-Specific locations run on single posts and pages, or on archive listings, and are tied to the post content.
  • eCommerce locations run at points that belong to a store plugin, such as a cart or a checkout form.

One more thing shapes the list: the snippet’s code type. The five locations under PHP Snippets Only accept PHP snippets alone.

On any other code type, those five appear greyed out. WPCode also moves that group to the bottom of the list. Every other group works with any code type.

Setting a Snippet’s Location

To get started, we’ll build a short HTML notice and set it to appear at the end of every post.

From your WordPress admin sidebar, go to Code Snippets » + Add Snippet.

Opening the Add Snippet screen from the Code Snippets menu in the WordPress admin sidebar

On the Snippet Library tab, hover over the Add Your Custom Code card. Then click + Add Custom Snippet.

An arrow pointing at the + Add Custom Snippet button on the Add Your Custom Code card, revealed on hover

WPCode asks which kind of snippet you’re building. For this example, click HTML Snippet.

An arrow pointing at the HTML Snippet card in the snippet type picker, beside the Text Snippet, Blocks Snippet, CSS Snippet, SCSS Snippet, and JavaScript Snippet cards

Note: Not sure which type fits your code? Our guide on what type of snippet you need walks through all eight options.

Add your code. Then scroll down to the Insertion panel.

Insert Method arrives set to Auto Insert. The Location control below it shows the current choice. To open the picker, click Location.

The Insertion panel with Insert Method set to Auto Insert, and an arrow pointing at the Location control reading Site Wide Header

The picker opens underneath. On the left is a search box and the three category names, with Global selected. On the right are the locations in that category, grouped and described.

The location picker open, showing the Search locations box and the Global, Page-Specific, and eCommerce categories beside the Site Wide Header, Site Wide Body, and Site Wide Footer locations

Our notice belongs on single posts, so in the rail on the left, click Page-Specific.

An arrow pointing at Page-Specific in the location picker's category rail, below the highlighted Global category

Note: With 65 locations to move through, the quickest route is often the Search locations box above the category names. Typing part of a location name filters the list as you go.

The list now shows the page-specific groups. Under Page, Post, Custom Post Type, click Insert After Content.

An arrow pointing at Insert After Content in the Page, Post, Custom Post Type group, beside Insert Before Post, Insert After Post, Insert Before Content, Insert Before Paragraph, and Insert After Paragraph

The picker closes and the Location control carries your choice.

The Insertion panel with Insert Method set to Auto Insert and the Location control now reading Insert After Content

Back at the top of the screen, click Save Snippet. Switch the snippet to Active whenever you’re ready for it to run.

An arrow pointing at the Save Snippet button at the top right of the snippet editor, beside the Inactive status toggle

That flow is the same whichever location you choose. The rest of this guide covers every location WPCode offers, grouped the way the picker groups them. That way, you know which one suits your own snippet.

Choosing a Global Location

Global locations aren’t tied to any single post. They cover the four groups below.

PHP Snippets Only

These five run earlier than anything else and can be selected on PHP snippets alone. They hook in before the page starts rendering. That is what you want for registering a post type, adding a filter, or anything else that has to be in place early.

  • Run Everywhere: the snippet runs everywhere on your website.
  • Frontend Only: the snippet runs only on the front end of the website.
  • Admin Only: the snippet runs only in the wp-admin area.
  • Frontend Conditional Logic: runs the snippet a little later on the front end, so that more conditional logic rules are available to it.
  • On Demand: the snippet never runs on its own. You trigger it yourself.

The PHP Snippets Only group in the location picker, listing Run Everywhere, Frontend Only, Admin Only, Frontend Conditional Logic, and On Demand with their descriptions

Site Wide (Frontend)

The three classic script positions, available to any code type. These are the right home for analytics tags, verification codes, and anything else that belongs on every page of the public site.

  • Site Wide Header: inserts the snippet between the head tags of your website on all pages.
  • Site Wide Body: inserts the snippet just after the opening body tag.
  • Site Wide Footer: inserts the snippet in the footer just before the closing body tag.

Admin Area

Two positions inside WordPress itself, for code meant for you and your team rather than your visitors.

  • Admin header: inserts the snippet in the wp-admin header area.
  • Admin footer: inserts the snippet in the wp-admin footer.

Anywhere

The Anywhere group aims at an HTML element you name with a CSS selector. This lets you reach spots your theme never gave you a hook for. Choosing one of these five adds a CSS Selector field to the panel.

  • Before HTML Element: inserts the snippet before the element the selector matches.
  • After HTML Element: inserts the snippet after the element.
  • At the start of HTML Element: inserts the snippet before the element’s own content.
  • At the end of HTML Element: inserts the snippet after the element’s own content.
  • Replace HTML Element: replaces the element completely with the snippet’s output.

Note: Our guide on using CSS selectors covers how to write the selector these locations need.

Choosing a Page-Specific Location

Page-specific locations attach the snippet to post content. They run on single posts and pages, or on the listings that show posts.

Page, Post, Custom Post Type

Six positions on a single post, page, or custom post type. This is the group we used in the walkthrough above. It’s the usual home for a call to action, a byline, or an inline ad.

  • Insert Before Post: inserts the snippet at the beginning of a post.
  • Insert After Post: inserts the snippet at the end of a post.
  • Insert Before Content: inserts the snippet at the beginning of the post content.
  • Insert After Content: inserts the snippet at the end of the post content.
  • Insert Before Paragraph: inserts the snippet before a paragraph number you choose.
  • Insert After Paragraph: inserts the snippet after a paragraph number you choose.

Categories, Archives, Tags, Taxonomies

Five positions on listing pages, where WordPress shows several posts at once rather than one. That covers your category, tag, and date archives as well as your blog page. A snippet here never appears on a single post, which is what the group above is for.

  • Insert Before Excerpt: inserts the snippet above the post summary.
  • Insert After Excerpt: inserts the snippet below the post summary.
  • Between Posts: inserts the snippet between multiple posts.
  • Before Post: inserts the snippet at the beginning of a post, at a position in the list you choose.
  • After Post: inserts the snippet at the end of that post.

Content

Eight positions measured against the length of the post, rather than against a fixed element. They suit long articles, where a third of the way in is more useful than a paragraph number that shifts every time you edit.

  • Insert After # Words: inserts the snippet after a minimum number of words.
  • Insert Every # Words: inserts the snippet again every time that many more words go by.
  • Insert in the Middle of the Content: inserts the snippet halfway through the post content.
  • Insert after first Quarter (25%): inserts the snippet after the first quarter of the post content.
  • Insert after 3rd Quarter (75%): inserts the snippet after the third quarter of the post content.
  • Insert after first Third (33%): inserts the snippet after the first third of the post content.
  • Insert after second Third (66%): inserts the snippet after the second third of the post content.
  • Insert after 80%: inserts the snippet after 80% of the post content.

Choosing an eCommerce Location

The eCommerce category holds 31 locations across three store plugins. WPCode always lists all three groups so you can see what’s on offer. It marks a group Not Installed when its plugin isn’t running on your site.

Once you install and activate the matching plugin, that group’s locations become selectable.

WooCommerce

13 positions covering product lists, single products, the cart, the checkout, and the thank you page.

  • Before the List of Products and After the List of Products: around the product list on a WooCommerce page.
  • Before the Single Product and After the Single Product: around the content on a single product page.
  • Before the Single Product Summary and After the Single Product Summary: around the product summary on that page.
  • Before the Cart and After the Cart: around the cart.
  • Before the Checkout Form and After the Checkout Form: around the checkout form.
  • Before Checkout Payment Button and After Checkout Payment Button: around the payment button on the checkout.
  • Before the Thank You Page: before the content of the thank you page.

The WooCommerce group in the location picker carrying a Not Installed label, with its greyed-out product list and single product locations

Easy Digital Downloads

Ten positions covering purchase buttons, single downloads, the cart, and the checkout.

  • Before the Purchase Button and After the Purchase Button: around the purchase button.
  • Before the Single Download and After the Single Download: around the content of a single download.
  • Before the Cart and After the Cart: around the cart.
  • Before the Checkout Cart and After the Checkout Cart: around the cart shown on the checkout.
  • Before the Checkout Form and After the Checkout Form: around the checkout form.

MemberPress

Eight positions covering registration, checkout, the account page, the login form, and unauthorized messages.

  • Before the Registration Form: before the registration form used for checkout.
  • Before Checkout Submit Button: right before the checkout submit button.
  • Before Checkout Coupon Field: before the checkout coupon field.
  • Before Account First Name: on the Home tab of the account page, before the First Name field.
  • Before Subscriptions Content: at the start of the Subscriptions tab on the account page.
  • Before Login Form Submit: on the login page, before the Remember Me checkbox.
  • Before the Unauthorized Message and After the Unauthorized Message: around the message shown to visitors without access.

Filling In the Extra Location Fields

Most locations describe a spot precisely enough on their own. A handful need one more detail from you. WPCode adds the field to the Insertion panel as soon as you pick the location.

A Number

Six locations count something and need to know how far to count: Insert Before Paragraph, Insert After Paragraph, Before Post, After Post, Insert After # Words, and Insert Every # Words. The field’s label changes to match the location. Here it reads “after paragraph number.”

The Location control set to Insert After Paragraph, with the after paragraph number field below it outlined in red and holding the value 3

A CSS Selector

All five Anywhere locations ask which element to target. To do this, type the selector into CSS Selector if you already know it.

Next to that field sits Pick from page. It opens your live site in a new browser tab with a picker bar across the top. You can click the element itself and let WPCode fill the selector in for you.

Underneath sits Element index, which decides what happens when the selector matches more than one element. Leave it blank, where it reads All, to target every match. Enter an index to target a single one.

The Location control set to Before HTML Element, with the CSS Selector field outlined in red holding a selector, the Pick from page button beside it, and the Element index field reading All below

A Trigger, for On Demand

On Demand is the one location that never fires by itself. The Active toggle at the top of the editor makes no sense for it, so WPCode hides it. In its place, you get Execute Snippet Now, which saves the snippet and runs it once.

An arrow pointing at the Execute Snippet Now button in the snippet editor header, beside the Save Snippet button, with the Active toggle no longer shown

WPCode reloads the editor afterwards with a notice reading “Snippet updated & executed.”, which is how you know the run happened.

You can also run the snippet from your own code with wpcode_get_snippet( $id )->execute();. That is what makes On Demand useful for tasks you want to fire on a schedule or from another plugin.

Note: Our guide on running code snippets on demand goes further into this location.

That’s it! You now know what every WPCode location does and how to point a snippet at the one you need.

Next, take a look at our guide on using smart conditional logic, which narrows a location down further by page, user, or date.

Frequently Asked Questions

Below, we’ve answered some of the most common questions about WPCode snippet locations.

Can I use the same snippet in more than one location?

No. Each snippet stores a single location. To place the same code in two spots, create a second snippet. You can also set Insert Method to Shortcode and paste the shortcode wherever you need it.

Why is the PHP Snippets Only group greyed out?

Those five locations run before the page renders, which only PHP snippets can do. On any other code type, WPCode disables them and moves the group to the bottom of the list. Switch the snippet’s code type to PHP and they become selectable.

What happens if I want a WooCommerce location but the group says Not Installed?

WPCode lists the store groups whether or not the matching plugin is running, so you can see what each one offers. The locations become selectable once that plugin is installed and activated on your site.

Why did WPCode change my location to Frontend Conditional Logic?

On a PHP snippet set to Run Everywhere, turning on Smart Conditional Logic switches the location to Frontend Conditional Logic. WPCode tells you when it makes that change. Run Everywhere fires before WordPress knows which page it is about to show, so many rules cannot be evaluated yet. The later location gives them a full picture.

You can switch back to Run Everywhere if you need the snippet to run early. Fewer conditional logic options will be available. Our guide on conditional logic with PHP snippets covers this in detail.

Does the location decide where a shortcode appears?

No. Switching Insert Method to Shortcode takes the location out of the picture entirely. The snippet runs wherever the shortcode is placed. Our guide on shortcode attributes covers that mode.

How do I find a location quickly?

To narrow the list fast, use the Search locations box at the top of the picker’s left rail. It filters the list as you type and searches group names as well as location names. Typing “cart” brings up the cart positions from both the WooCommerce and Easy Digital Downloads groups at once, whichever category you started in.

Still stuck? How can we help?

Last Updated on

Get Started Today & Add Your Own Snippets

Future-proof your website with WPCode Snippets and improve the way you manage code across all your websites.