Last updated on September 13, 2023

Add Code to Body

Don’t know where to add this snippet? Read our guide: How to add code snippets.

Add custom code to the body.

Adding code to the body section of a WordPress site can be useful for a variety of purposes, such as adding custom HTML elements, modifying the content of a specific page or post, or adding content to a shortcode. To add code to the body section of a WordPress site, you can use WordPress filters or shortcodes.

Option 1: Add code using wp_body_open action

If your theme calls the wp_body_open() function after the <body> opening tag, then you can add code in this position using the wp_body_open action hook.

add_action( 'wp_body_open', 'wpsnippets_add_code_to_body' );

function wpsnippets_add_code_to_body( $content ) {

    // Check for a specific post
    if ( is_single( 123 ) ) {

        // Add custom HTML to the content
        echo '<div class="custom-content">Custom HTML content goes here.</div>';
    }
}

In this example, we use the wp_body_open action hook to call the wpsnippets_add_code_to_body() function, which outputs custom HTML. Please note: the output of this function will be inserted after the body open tag. If you need to insert code in a different position, use one of the other options below.

Option 2: Add code to body using a WordPress filter

Here is an example code snippet that adds custom HTML to the body section of a specific page or post using the the_content filter:

add_filter( 'the_content', 'wpsnippets_add_code_to_body' );

function wpsnippets_add_code_to_body( $content ) {

    // Check for a specific post
    if ( is_single( 123 ) ) {

        // Add custom HTML to the content
        $custom_html = '<div class="custom-content">Custom HTML content goes here.</div>';
        $content .= $custom_html;
    }

    return $content;
}

In this code snippet, we define a function wpsnippets_add_code_to_body that checks if the current page or post is the desired one (in this case, with the ID 123), and if so, adds a custom HTML element to the content of the page or post. We then return the modified content using the $content parameter, which is passed by the the_content filter. Finally, we hook this function to the the_content filter using the add_filter() function.

You can modify this code snippet according to your specific needs, such as adding different HTML elements or modifying the content of different pages or posts.

Option 3: Add code to body using a shortcode

Here is an example code snippet that adds a shortcode that displays custom content in the body section of a page or post:

add_shortcode( 'custom_shortcode', 'wpsnippets_custom_shortcode' );

function wpsnippets_custom_shortcode() {
    return '<div class="custom-content">Custom HTML content goes here.</div>';
}

In this code snippet, we define a function wpsnippets_custom_shortcode that returns the custom HTML element we want to add to the body section. We then use the add_shortcode() function to create a shortcode with the name custom_shortcode that will call this function when used in the content of a page or post.

To use this shortcode in the content of a page or post, simply add the shortcode tag [custom_shortcode] to the content. When the page or post is rendered, the shortcode will be replaced with the HTML element returned by the wpsnippets_custom_shortcode function.

You can modify this code snippet according to your specific needs, such as adding different HTML elements or modifying the shortcode name.

Last updated on September 13, 2023. Originally posted on May 7, 2023.

Leave a Reply

Your email address will not be published. Required fields are marked *