Add script to head tag in WP

Last Updated:June 11, 2021

If you are WordPress Plugin Deveveloper you may need to add some text or code inside the <head> tag

Here is how you can do it:

You can use the wp_head action hook which is triggered within the <head></head> section of the theme’s header.php template by the wp_head() function.

First create the custom function

function fn_head_script() {
     if ( is_page() ) {
            alert('Page is loading...');

The you can call the action hook

add_action('wp_head', 'fn_head_script');

This is very usefull when you are going to develop custom styles and structured data markup