---
title: "How to Use the is_preview() Function"
description: "In this article, we will introduce the WordPress is_preview() function. This is how you can effectively edit what content is generated when you preview a page or post. Most commonly, this can be used..."
url: https://www.inmotionhosting.com/support/edu/wordpress/the-is-preview-function/
date: 2017-11-08
modified: 2023-06-09
author: "Christopher Maiorana"
categories: ["WordPress Tutorials"]
type: post
lang: en
---

# How to Use the is_preview() Function

In this article, we will introduce the WordPress **is_preview() function**. This is how you can effectively edit what content is generated when you preview a page or post. Most commonly, this can be used to differentiate the preview page from published pages. This is especially helpful if you are using something like Google analytics to track pageviews.

The is_preview() function is a conditional tag. So, using the example above, if you don’t want to track pageviews for preview pages, this is the function you will want to use.

## How to use the is_preview() function

If you are using a theme designed by someone else, it would be best to make this modification in a [child theme](/support/edu/wordpress/wp-childtheme-2014/). However, if you are using a custom theme, you’re ready to get started.

The directory we will be working with is the theme directory for your theme. This will be slightly different depending on your theme, but the location is always the same: */wp-content/themes/name-of-theme*. In this case, “name-of-theme” is substituted for the name of the theme you’re working with.

Also, we will be using the cPanel File Manager to edit our theme files.

In this example, we are going to instruct WordPress to only include our analytics code on pages that are *not* preview pages.

1. [![cPanel login screen](/support/images/stories/cpanel/lanternUpdatesMay17/cmPape_03.png)](/support/images/stories/cpanel/lanternUpdatesMay17/cmPape_03.png)[Log into cPanel](/support/edu/cpanel/how-to-log-into-cpanel/)
2. imhCmpix_-36.png [![File Manager icon](/support/images/stories/wordpress-development/functions/imhCmpix_-36.png)](/support/images/stories/wordpress-development/functions/imhCmpix_-36.png)Under *Files* choose **File Manager**
3. imhCmpix_-37.png [![Themes directory](/support/images/stories/wordpress-development/functions/imhCmpix_-37.png)](/support/images/stories/wordpress-development/functions/imhCmpix_-37.png)Navigate to the theme directory listed above
4. imhCmpix_-38.png [![Click plus file link](/support/images/stories/wordpress-development/functions/imhCmpix_-38.png)](/support/images/stories/wordpress-development/functions/imhCmpix_-38.png)Create a file (by clicking the **+File** button) to contain your analytics code; for this example, we can name it *analytics-code.txt*
5. imhCmpix_-39.png [![analytics code text file](/support/images/stories/wordpress-development/functions/imhCmpix_-39.png)](/support/images/stories/wordpress-development/functions/imhCmpix_-39.png)Paste your analytics code into the “analytics-code.txt” file
6. imhCmpix_-40.png [![blue Save Changes button](/support/images/stories/wordpress-development/functions/imhCmpix_-40.png)](/support/images/stories/wordpress-development/functions/imhCmpix_-40.png)Click **Save Changes**
7. imhCmpix_-41.png [![gray Close button](/support/images/stories/wordpress-development/functions/imhCmpix_-41.png)](/support/images/stories/wordpress-development/functions/imhCmpix_-41.png)Click **Close**
8. imhCmpix_-42.png [![find header.php file](/support/images/stories/wordpress-development/functions/imhCmpix_-42.png)](/support/images/stories/wordpress-development/functions/imhCmpix_-42.png)Now open the `header.php` file by highlighting it and clicking the **Edit** button or using a right-click and choosing **Edit** (depending on your theme, the *header.php* file may be in a “template parts” directory
9. imhCmpix_-43.png [![add code to header.php file](/support/images/stories/wordpress-development/functions/imhCmpix_-43.png)](/support/images/stories/wordpress-development/functions/imhCmpix_-43.png)Add the following code just beneath the `<head>` section: <?php if ( ! is_preview() ) { include "analytics-code.txt"; } ?>

You may notice the exclamation point placed before the *is_preview()* function in the code snippet above. This means, if the page is *not* a preview page then insert our analytics code. This means all pages and posts will include the analytics code necessary for tracking purposes.

Well done! You now know how to use the **is_preview() function** in a conditional statement to modify your preview page in WordPress.
