How to Add Code After the Body Tag in WordPress

Add Code After the Body Tag in WordPress

Lets Add Code After the Body Tag in WordPress. The code is usually inserted after the body tag on the website. This is basically an unseen JavaScript code which is written in a script tag. The script which needs to be written before the closing of the head tag and after the opening of the body element is provided by some external services like Google Tag Manager, Google AdSense, Google Analytics, Facebook etc.

The function wp_head() is used to add code before closing of head tag in WordPress. WordPress also provides a wp_footer() function that adds code just before closing the body element (not after opening). Function wp_footer() is used by developers to add code in the body tag or inertly after opening the body element in WordPress. These two options are usually not recommended.

External services recommend adding code statically. By using this technique we are unable to update a theme. 

There is a new function (wp_body_open()) introduced by WordPress 5.2. This function is basically used to add code after opening the body element. How about we have a look at how to inject code just after opening the body tag in WordPress.

Add Code After the Body Tag in WordPress

If you are using the latest version of WordPress then it is recommended to add wp_body_open( ) method next to the body tag.

The syntax is shown below:

<body <?php body_class(); ?>>

<?php wp_body_open(); ?>

After this, the function wp_body_open() can be replaced by action wp_body_open to add your code. Or we can say, the code can be injected just after opening the body tag.

It is shown below:

add_action(‘wp_body_open’, ‘add_code_on_body_open’);

function add_code_on_body_open() {

    echo ‘<script>Your code here</script>’;

}

This code will work fine in WordPress version 5.2. But if you are using an older version of WordPress then wp_body_open can be used.

It is shown below:.

if ( ! function_exists( ‘wp_body_open’ ) ) {

    function wp_body_open() {

        do_action( ‘wp_body_open’ );

    }

}

Now, add the method wp_body_open() after the body element as shown in the above code.

According to your work you can add various scripts on pages, posts, or categories. WordPress allows you to apply conditions and put scripts easily and accordingly. To add various scripts on each page you can follow the code given below.

add_action(‘wp_body_open’, ‘add_code_on_body_open’);

function add_code_on_body_open() {

    if ( is_front_page() ) { // for home page

        echo ‘<script>Your code here</script>’;

    } elseif ( is_page(‘PAGE_SLUG’) ) { // for specific page

        echo ‘<script>Your code here</script>’;

    } elseif ( is_single() ) { // for single post

        echo ‘<script>Your code here</script>’;

    } elseif ( is_category() ) { // for category

        echo ‘<script>Your code here</script>’;

    }

}

This is a very operative and efficient way to add code after the body tag in WordPress. It is highly recommended to use this technique for the WordPress developers on their websites.

Share This Post...
Table of Contents

Add a Comment

Share This Post...