Last updated on September 13, 2023

Add Code To a Specific Page

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

Add code to a specific page.

To add custom code to a specific page in WordPress, you need to first identify the page’s ID. To find the page ID, go to Pages in the WordPress dashboard and hover over the page you want to add code to. The ID number will appear in the URL at the bottom of your browser. You can also click on Edit, and then see the post ID in the URL in the browser.

In WordPress, the post ID will appear in the URL when you edit a post in the WP Admin area.

Once you have the ID number, you can use the following code snippet to add code to the site:

add_filter( 'the_content', 'wpsnippets_add_code_to_page' );

function wpsnippets_add_code_to_page( $content ) {

    // Check for the page ID
    if ( is_page( 123 ) ) {

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

    return $content;
}
Last updated on September 13, 2023. Originally posted on May 8, 2023.

Leave a Reply