If you need your snippet to load at a specific point in the WordPress load order, wrap your snippet code in an add_action hook.

For example, to load a snippet when the page footer is loaded, you could use the wp_footer hook:

add_action( 'wp_footer', function () {

    // code here

} );

You can read more about action hooks in the WordPress Code Reference and see a full list in the WordPress Codex.

Here are some common examples:

  • after_setup_theme – just after the theme is loaded
  • init – after WordPress has loaded, but before any content has loaded
  • wp_head – in the site <head> section, where scripts and styles are output
  • wp_body_open – just after the opening <body> tag
  • wp_footer – just before the closing </body> tag