• Resolved dangerdanj

    (@dangerdanj)


    I added a javascript code snippet to the sitewide footer but the code is displaying above a bunch of other scripts and I need it to be after a specific script.

    Is there a way to force it to be immediately before the closing body tag and after everything else?

Viewing 8 replies - 1 through 8 (of 8 total)
  • Thread Starter dangerdanj

    (@dangerdanj)

    To give more detail… I tried setting the priority to 1 but that didn’t move it down far enough.

    I’m using Elementor Pro and they are loading the Swiper library right before the closing body tag. I need to get my code below that.

    I tried using the custom code section of Elementor Pro with a priority of 1, and that gets it down further, but not below the Swiper library.

    Plugin Author Mircea Sandu

    (@gripgrip)

    Hi @dangerdanj,

    For that scenario you will need to add the script manually to the footer using a PHP snippet and update the priority for that action in order to place the script where you need it. The WPCode priority changes the order just related to other snippets added by WPCode, not the priority used for the hook.

    You can try this code for a PHP snippet set to “Run Everywhere”:

    add_action( 'wp_footer', function() {
    	?> 
    	<script>
    	</script>
    	<?php
    }, 5 );

    Add your custom JS between the script tags and update the 5 value of the priority to change when the script is loaded.

    • This reply was modified 11 months, 2 weeks ago by Mircea Sandu.
    Thread Starter dangerdanj

    (@dangerdanj)

    Thank you for the response. Unfortunately, that code didn’t help. It adds my code way above the scripts that are already being loaded. My code needs to be the very last thing before the closing body tag.

    Plugin Author Mircea Sandu

    (@gripgrip)

    In that case you can try changing the 5 priority in the code to something higher like 1000 or higher.

    Thread Starter dangerdanj

    (@dangerdanj)

    I tried using a priority of 9999999999999999999999999 and still can’t get it to the bottom. There are two scripts that Elementor is loading below my code.

    Plugin Author Mircea Sandu

    (@gripgrip)

    Hi @dangerdanj,

    I see, in that case it might be that the Elementor scripts are loading the Swiper library using JavaScript and thus adding the scripts to the page once the JS is executed and in that case there’s no way for you to load a script later using PHP and you might need to find a different approach.

    Hy i have the code in the header and in the body but it does not get the higher Priority (100) in both code Snippet?but in the both time it shows the lower end how can I overcame for this

    • This reply was modified 9 months, 1 week ago by aneess76.
    Plugin Author Mircea Sandu

    (@gripgrip)

    Hi @aneess76,

    The priority in the WPCode snippet settings only has an effect on the order in which the snippet is loaded in relation with other WPCode snippets.

    If you want to load a script earlier than the priority used by the WPCode Site Wide Header location you will need to use a custom PHP snippet and use that to load your script, here’s an example:

    add_action( 'wp_head', function() {
    	?>
    	<script>// Your script here</script>
    	<?php
    }, 5);

    You can adjust 5 in this example to achieve the desired result – a lower number will get loaded earlier.

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Place code immediately before closing body tag’ is closed to new replies.