In a previous article we have seen how to manually concatenate scripts in your main theme to avoid loading multiple files.
In case you can’t concatenate some scripts here are two options to defer the exectution of the script until after the page has fully loaded or simply load a php script in the footer rather than in the header of your page.
Defer the execution of a script:
For your inline scripts:
<script defer> your script</script>
And it is exactly the same when the scripts are in an external file:
<script src="/path/to/your/script" defer></script>
Execute a function in the footer instead of the header.
In one of my plugn I had the case where I needed to fetch all the categories and product variation in jquery plugin that allow live display and filtering for a given search string. As such, the jquery is prepared in a php file that I then included in my theme header with and include function. Until I realize I can as easily load the script in the footer and avoid to overload my header.
In the index.php file of the plugin:
add_action('wp_header', 'PluginName::functionName);
simply becomes:
add_action('wp_footer', 'PluginName::functionName);
functionName
is your plugin function that contains the include statement that should be loaded in the footer.