Integrate external .js file

Hi There,

what’s the correct way to integrate an .js file, that is hosted somewhere else?

The link I have to use is:
<script src="https://www.###URL###.de/modul/JS/AdvancedIframeResizerHost.js"></script>

I found several descriptions how to integrate a file uploaded to my child theme, but this does not help.

Is there any manual, how to do that?

Best regards

Uli

Hello Uli,

Ideally, you will have to load the external JS file through the wp_enqueue_script function in the child theme.

https://developer.wordpress.org/reference/functions/wp_enqueue_script/

If you want to go that route, please install and activate the child theme and login through FTP then edit the functions.php then add this code:

 wp_enqueue_script( 'my-external-js', 'https://www.###URL###.de/modul/JS/AdvancedIframeResizerHost.js', '', '', false );

Another option would be to load the script via the wp_head filter. To do that, please add this code in the functions.php file:

add_action('wp_head', 'add_header_code');
function add_header_code(){
?>
    <script src="https://www.###URL###.de/modul/JS/AdvancedIframeResizerHost.js"></script>
    <?php
};

Hope this helps.

Hi Jade, yeah, that helped, thank you very much.

There is just one typo in your code, a missing ’ after the URL, here the correct code for other users:

wp_enqueue_script( 'my-external-js', 'https://www.###URL###.de/modul/JS/AdvancedIframeResizerHost.js', '', '', false );

Have a nice weekend

Uli

Ah, thanks for pointing that out and glad to hear it’s sorted. Uli.

Have a nice weekend. :slight_smile:

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.