How to make a snippet run on only one page


By Shea Bunge

updated about 1 year ago

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

} );

Did this answer your question?