Adding Snippets

Everything you need to know about creating different sorts of snippets.

How to make a snippet run once per day

By default, snippets run every time your site is visited. If you want a snippet to run only once per day, you can create a cron job. Here's an example: add_action( 'init', function () { $hook = 'run_snippet_daily'; $args = array(); if ( !...

Avatar

By Shea Bunge

updated over 1 year ago

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...

Avatar

By Shea Bunge

updated over 1 year ago

How to add Google Analytics tracking code

When adding the Global Site Tag (gtag.js) script to your site for Google Analytics tracking, you want to put the tracking code just after the opening <head> tag. You can achieve this using the wp_head action hook. Here's an example:...

Avatar

By Shea Bunge

updated over 1 year ago

How to add Microsoft Clarity tracking code

If you want to add Microsoft Clarity tracking to your site, you will be asked to add some code to your site's   <head> tag. The easiest way to do this is add a snippet that hooks to the wp_head action. Here's an example: add_action( 'wp_head',...

Avatar

By Shea Bunge

updated over 1 year ago

How to add a Facebook Pixel

Adding a Facebook Pixel to your site is easy with when using Code Snippets. When setting up the pixel, select Install code manually , and then create a snippet similar to the one below including your pixel base code: add_action( 'wp_head',...

Avatar

By Shea Bunge

updated over 1 year ago

Using AJAX within Snippets to combine PHP and JavaScript

Sometimes when you are working with snippets you'll encounter a scenario where you have a value in JavaScript that you want to pass back to a PHP function. One possible solution to this issue is to make use of AJAX. This article is intended to...

Avatar

By Shea Bunge

updated 7 months ago