InMotion Hosting Support Center

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. 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. Log into cPanel
  2. Under Files choose File Manager
  3. Navigate to the theme directory listed above
  4. Create a file (by clicking the +File button) to contain your analytics code; for this example, we can name it analytics-code.txt
  5. Paste your analytics code into the "analytics-code.txt" file
  6. Click Save Changes
  7. Click Close
  8. 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. 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.

Was this article helpful?

Related Questions

Here are a few questions related to this article that our customers have asked:
Ooops! It looks like there are no questions about this page.
Would you like to ask a question about this page? If so, click the button below!
Ask a Question

Support Center Login

Our Login page has moved. Please click the button below to be redirected to the login page.

Post a Comment

Email Address:
Phone Number:

Please note: Your name and comment will be displayed, but we will not show your email address.

0 Questions & Comments

Post a comment

Back to first comment | top

Need more Help?


Ask the Community!

Get help with your questions from our community of like-minded hosting users and InMotion Hosting Staff.

Current Customers

Chat: Click to Chat Now E-mail:
Call: 888-321-HOST (4678) Ticket: Submit a Support Ticket

Not a Customer?

Get web hosting from a company that is here to help. Sign up today!