Using wp_add_inline_script function. A Google Analytics example.

Until version version 4.4, in WordPress, there was only one way to add script files to the WordPress themes, the function wp_enqueue_script however, the new version 4.5 has introduced a new way, the function wp_add_inline_script that allows us to add javascript text scripts directly inside a template and that, in addition, it can be directly connected with other scripts. Let’s go to see how it works.

Example of using the wp_add_inline_script function

At bottom, this new function wp_add_inline_script is the equivalent for scripts files, to the wp_add_inline_script function is for CSS files. Let’s see how it works through and example based on adding the Google Analytics script to a WP theme. Continue reading…