0

I don't deal with Wordpress or PHP really at all, our dev left last year and we've just muddled through really.

I'm fine with adding in scripts and what not, but this specific script has to be the first script loaded under the head tag.

I can't for the life of me get it to work, every question I've read on here offers the same advice (which I've tried) using enqueuing and it's just not changing anything.

I've also tried adding the snippet directly under the tag in the header.php file, no joy. It's almost as though something else is forcing the other scripts to be ordered above it.

The script I've tried:

function my_custom_js() {
    echo '<script type="text/javascript"> window.hfAccountId = "ACCOUNT_ID";
    window.hfDomain = "DOMAIN";
    (function() {
        var hf = document.createElement("script"); hf.type = "text/javascript"; hf.async = true;
        hf.src = window.hfDomain + "/scripts/hf.js";
        var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(hf, s);
    })();
</script> ';
}

// Add hook for admin <head></head>
add_action( 'admin_head', 'my_custom_js', -1000 );
// Add hook for front-end <head></head>
add_action( 'wp_head', 'my_custom_js', -1000 );

This is just one of some variations I've tried, this one specifically I read to put in the functions.php file, so I did.

I've also tried adding to the functions > enqueue.php file... no joy.

It appears perfectly fine on the site, just not in the right place.

Please help!

kate
  • 1
  • `wp_enqueue_script` is made to enqueue script resources in separate files, to be loaded via URL - your script code here is "inline" script though. WP also provides `wp_add_inline_script` for stuff like this - but with that, you can only _append_ inline script to an already registered external script resource. – CBroe Mar 31 '23 at 11:35
  • I would try and use `wp_print_scripts`, as explained here, https://digwp.com/2019/07/better-inline-script/ – CBroe Mar 31 '23 at 11:36
  • I've tried with wp_print_scripts and it appears the same way it did when I used the enqueue, but still not directly below the `` tag. – kate Mar 31 '23 at 12:31
  • _"but still not directly below the tag"_ - but ...? What _is_ the actual issue - other scripts getting output before it, or just standard stuff like title and meta elements? – CBroe Mar 31 '23 at 12:37
  • So essentially we're onboarding a bit of kit which needs us to implement this script. The issue isn't that the script isn't showing in the head tags, because it is, it's that it's not the first thing showing directly beneath the opening head tag. They require it to be the first script on the page which is massively frustrating. Can't for the life of me find where the scripts are coming from that are being outputted before it - which made me think it could be a plugin loading them in, but on checking the plugins there's none that should be loading scripts. – kate Mar 31 '23 at 12:49
  • If plugins are inserting their "own" scripts from within their respective plugin directory, that should be pretty apparent due to the URL paths you'd see then already. If anything with outside CDN URLs gets added, then you should probably be able to find those by doing a search over the complete project folder in your IDE. And if that doesn't help, you can still disable them all, and see if the problem persists. – CBroe Mar 31 '23 at 13:03
  • 3
    _"They require it to be the first script on the page"_ - whether that's an _actual_ technical necessity, or just something that provider "wants", might still be a different question. Often times they just "demand" that for no other reason than that they want _their_ scripts to load as early as possible, so that if multiple external widgets or whatever are contained on a page, theirs would show up as early as possible ... – CBroe Mar 31 '23 at 13:06

0 Answers0