A Bundle of Love - Get one year of Dynamic Shortcodes + Code Snippets Pro - for just $39
View Offer
A Bundle of Love - Get one year of Dynamic Shortcodes + Code Snippets Pro - for just $39
View Offer

How to make a snippet run on only one page

You can restrict a snippet to only running on one page of your site by adding this line near the top:

if ( ! is_page( 'Page Name' ) ) return;

You can replace Page Name with the title, slug, or ID of the page you want to run the snippet on, or even include multiple IDs, names or slugs in an array. Here’s an example:

add_action( 'wp_footer', function () {
    if ( ! is_page( array( 42, 'Page Name', 'about-us', 968 ) ) ) return;

    // rest of snippet code here

} );

Related Articles