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 loadedinit
– after WordPress has loaded, but before any content has loadedwp_head
– in the site<head>
section, where scripts and styles are outputwp_body_open
– just after the opening<body>
tagwp_footer
– just before the closing</body>
tag