How to Run Code Snippets on Specific Pages in WordPress
Content
A common challenge for WordPress users is controlling where a code snippet runs. By default, a snippet added with the Code Snippets plugin will execute on every page of a website, which can cause conflicts, slow down page loads, or simply be unnecessary. Many users want to target their CSS, JavaScript, or PHP code to specific pages, such as a homepage, contact page, or landing page.
Why This Happens
The Code Snippets plugin is designed to insert code globally across a WordPress site. It doesn't natively include a point-and-click interface for selecting where a snippet should run. This design means that without adding conditional logic, a snippet will load on the front-end, back-end, or everywhere, depending on its scope, but not on specific pages.
The Solution: Using WordPress Conditional Tags
The most effective and widely supported method is to wrap your snippet's code in a conditional check using native WordPress functions. This approach gives you precise control over the pages where your snippet is active.
Method 1: For CSS, JavaScript, or HTML Snippets (Output in the <head>)
If your snippet outputs code directly to the <head> section (like inline CSS or JavaScript), you can use the wp_head action hook along with a conditional check.
add_action( 'wp_head', function () {
// Replace 42 with your page ID, slug, or title
if ( ! is_page( 42 ) ) {
return; // Exit if it's not the correct page
}
?>
<style>
/* Your CSS code here */
</style>
<?php
} );
Method 2: For PHP Function Snippets
If your snippet is a PHP function that modifies site behavior (e.g., a filter for WooCommerce), place the conditional check at the very beginning of the function.
add_filter( 'some_filter_hook', function ( $value ) {
// Run this snippet ONLY on the page with ID 18
if ( ! is_page( 18 ) ) {
return $value; // Exit and return the original value
}
// Your modification code here
return $modified_value;
} );
Targeting Multiple Pages or Using Different Conditions
You can easily expand these examples to target multiple pages or use different conditional tags.
add_action( 'wp_head', function () {
// Run on pages with ID 123, 456, and the 'about' page slug
if ( ! is_page( [ 123, 456, 'about' ] ) ) {
return;
}
?>
<script>
/* Your JavaScript code here */
</script>
<?php
} );
Other useful conditional functions include:
is_front_page()- Targets the site's front page.is_single( 'post-slug' )- Targets a specific post.is_post_type_archive( 'product' )- Targets a post type archive.
Important Considerations
- Syntax is Critical: A missing parenthesis or quote (like
is_page( 'richlands' )vs.is_page( richlands' )) will cause the snippet to break. - Scope Matters: Conditional tags like
is_page()only work correctly on the front-end. For admin-area snippets, use theadmin_enqueue_scriptshook and check the$hookorget_current_screen(). - This is a Workaround: The community has frequently requested a built-in feature for this. The Code Snippets team has indicated this functionality is planned for a future release.
By integrating these simple conditional checks, you can gain fine-grained control over your snippets, improving your site's performance and preventing unwanted conflicts.
Related Support Threads Support
-
Running CSS snippets to a specific page onlyhttps://wordpress.org/support/topic/running-css-snippets-to-a-specific-page-only/
-
Javascript only on one pagehttps://wordpress.org/support/topic/javascript-only-on-one-page/
-
Snippet on single page?https://wordpress.org/support/topic/snippet-on-single-page/
-
How do I add a Javascript Snippet to a particular page or pages a few pageshttps://wordpress.org/support/topic/how-do-i-add-a-javascript-snippet-to-a-particular-page-or-pages-a-few-pages/
-
Embed Page ID into Code Snippet for displaying full HTMLhttps://wordpress.org/support/topic/embed-page-id-into-code-snippet-for-displaying-full-html/
-
frontend editing for one pagehttps://wordpress.org/support/topic/frontend-editing-for-one-page/
-
PONER UN CODIGO EN LA CABECERA DE SOLO EN UNA PÁGINAhttps://wordpress.org/support/topic/poner-un-codigo-en-la-cabecera-de-solo-en-una-pagina/
-
Load JS on specific page in administrator areahttps://wordpress.org/support/topic/load-js-on-specific-page-in-administrator-area/
-
Adding Code as right floating sidebarhttps://wordpress.org/support/topic/adding-code-as-right-floating-sidebar/
-
Load Snippet on only a specified pagehttps://wordpress.org/support/topic/load-snippet-on-only-a-specified-page-2/
-
Load Snippet on only a specified pagehttps://wordpress.org/support/topic/load-snippet-on-only-a-specified-page/
-
Insert snippet into page body?https://wordpress.org/support/topic/insert-snippet-into-page-body/
-
Snippet only appears in home pagehttps://wordpress.org/support/topic/snippet-only-appears-in-home-page/
-
Target a Pagehttps://wordpress.org/support/topic/target-a-page/
-
Exclude a snippet on single specific pagehttps://wordpress.org/support/topic/exclude-a-snippet-on-single-specific-page/
-
Run PHP script on a single page/when called by Javascripthttps://wordpress.org/support/topic/run-php-script-on-a-single-page-when-called-by-javascript/
-
Run snippet on a specific page onlyhttps://wordpress.org/support/topic/run-snippet-on-a-specific-page-only/
-
Google Phone Snippethttps://wordpress.org/support/topic/google-phone-snippet/
-
How to turn on/off a snippet from a pagehttps://wordpress.org/support/topic/how-to-turn-on-off-a-snippet-from-a-page/
-
Add Code Snippets in Cornerstonehttps://wordpress.org/support/topic/add-code-snippets-in-cornerstone/
-
Snippet on all pages, but not firing on home pagehttps://wordpress.org/support/topic/snippet-on-all-pages-but-not-firing-on-home-page/
-
Only one pagehttps://wordpress.org/support/topic/only-one-page-2/
-
How to add a code snippet only in two pages?https://wordpress.org/support/topic/how-to-add-a-code-snippet-only-in-two-pages/
-
Run a code snippet in a specific page or at a specific placehttps://wordpress.org/support/topic/run-a-code-snippet-in-a-specific-page-or-at-a-specific-place/
-
Specifying Page(s)https://wordpress.org/support/topic/specifying-pages/
-
Snippet on Elementor Page without header or footerhttps://wordpress.org/support/topic/snippet-on-elementor-page-without-header-or-footer/
-
how to use my snippet of css only on 1 pagehttps://wordpress.org/support/topic/how-to-use-my-snippet-of-css-only-on-1-page/
-
Apply a snippet on one page onlyhttps://wordpress.org/support/topic/apply-a-snippet-on-one-page-only/