{"id":4236,"date":"2017-11-08T22:23:10","date_gmt":"2017-11-08T22:23:10","guid":{"rendered":"https:\/\/www.inmotionhosting.com\/support\/2017\/11\/08\/the-is-preview-function\/"},"modified":"2023-06-09T13:21:12","modified_gmt":"2023-06-09T17:21:12","slug":"the-is-preview-function","status":"publish","type":"post","link":"https:\/\/www.inmotionhosting.com\/support\/edu\/wordpress\/the-is-preview-function\/","title":{"rendered":"How to Use the is_preview() Function"},"content":{"rendered":"<p>In this article, we will introduce the WordPress <strong>is_preview() function<\/strong>. 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.<\/p>\n<p>The is_preview() function is a conditional tag. So, using the example above, if you don\u2019t want to track pageviews for preview pages, this is the function you will want to use.<\/p>\n<h2>How to use the is_preview() function<\/h2>\n<p>If you are using a theme designed by someone else, it would be best to make this modification in a <a href=\"\/support\/edu\/wordpress\/wp-childtheme-2014\/\">child theme<\/a>. However, if you are using a custom theme, you\u2019re ready to get started.<\/p>\n<p>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: <em>\/wp-content\/themes\/name-of-theme<\/em>. In this case, \u201cname-of-theme\u201d is substituted for the name of the theme you\u2019re working with.<\/p>\n<p>Also, we will be using the cPanel File Manager to edit our theme files.<\/p>\n<p>In this example, we are going to instruct WordPress to only include our analytics code on pages that are <em>not<\/em> preview pages.<\/p>\n<ol class=\"article_list\">\n<li><a href=\"\/support\/images\/stories\/cpanel\/lanternUpdatesMay17\/cmPape_03.png\" rel=\"lightbox-0\"><img decoding=\"async\" class=\"std_ss\" style=\"float: right; margin: 0px 15px 15px 15px; width: 200px;\" src=\"\/support\/images\/stories\/cpanel\/lanternUpdatesMay17\/cmPape_03.png\" alt=\"cPanel login screen\"><\/a><a href=\"\/support\/edu\/cpanel\/how-to-log-into-cpanel\/\">Log into cPanel<\/a>\n<div style=\"clear: both;\"><\/div>\n<\/li>\n<li><!-- imhCmpix_-36.png --><a href=\"\/support\/images\/stories\/wordpress-development\/functions\/imhCmpix_-36.png\" rel=\"lightbox-0\"><img decoding=\"async\" class=\"std_ss\" style=\"float: right; margin: 0px 15px 15px 15px; width: 200px;\" src=\"\/support\/images\/stories\/wordpress-development\/functions\/imhCmpix_-36.png\" alt=\"File Manager icon\"><\/a>Under <em>Files<\/em> choose <b>File Manager<\/b>\n<div style=\"clear: both;\"><\/div>\n<\/li>\n<li><!-- imhCmpix_-37.png --><a href=\"\/support\/images\/stories\/wordpress-development\/functions\/imhCmpix_-37.png\" rel=\"lightbox-0\"><img decoding=\"async\" class=\"std_ss\" style=\"float: right; margin: 0px 15px 15px 15px; width: 200px;\" src=\"\/support\/images\/stories\/wordpress-development\/functions\/imhCmpix_-37.png\" alt=\"Themes directory\"><\/a>Navigate to the theme directory listed above\n<div style=\"clear: both;\"><\/div>\n<\/li>\n<li><!-- imhCmpix_-38.png --><a href=\"\/support\/images\/stories\/wordpress-development\/functions\/imhCmpix_-38.png\" rel=\"lightbox-0\"><img decoding=\"async\" class=\"std_ss\" style=\"float: right; margin: 0px 15px 15px 15px; width: 200px;\" src=\"\/support\/images\/stories\/wordpress-development\/functions\/imhCmpix_-38.png\" alt=\"Click plus file link\"><\/a>Create a file (by clicking the <b>+File<\/b> button) to contain your analytics code; for this example, we can name it <em>analytics-code.txt<\/em>\n<div style=\"clear: both;\"><\/div>\n<\/li>\n<li><!-- imhCmpix_-39.png --><a href=\"\/support\/images\/stories\/wordpress-development\/functions\/imhCmpix_-39.png\" rel=\"lightbox-0\"><img decoding=\"async\" class=\"std_ss\" style=\"float: right; margin: 0px 15px 15px 15px; width: 200px;\" src=\"\/support\/images\/stories\/wordpress-development\/functions\/imhCmpix_-39.png\" alt=\"analytics code text file\"><\/a>Paste your analytics code into the \u201canalytics-code.txt\u201d file\n<div style=\"clear: both;\"><\/div>\n<\/li>\n<li><!-- imhCmpix_-40.png --><a href=\"\/support\/images\/stories\/wordpress-development\/functions\/imhCmpix_-40.png\" rel=\"lightbox-0\"><img decoding=\"async\" class=\"std_ss\" style=\"float: right; margin: 0px 15px 15px 15px; width: 200px;\" src=\"\/support\/images\/stories\/wordpress-development\/functions\/imhCmpix_-40.png\" alt=\"blue Save Changes button\"><\/a>Click <b>Save Changes<\/b>\n<div style=\"clear: both;\"><\/div>\n<\/li>\n<li><!-- imhCmpix_-41.png --><a href=\"\/support\/images\/stories\/wordpress-development\/functions\/imhCmpix_-41.png\" rel=\"lightbox-0\"><img decoding=\"async\" class=\"std_ss\" style=\"float: right; margin: 0px 15px 15px 15px; width: 200px;\" src=\"\/support\/images\/stories\/wordpress-development\/functions\/imhCmpix_-41.png\" alt=\"gray Close button\"><\/a>Click <b>Close<\/b>\n<div style=\"clear: both;\"><\/div>\n<\/li>\n<li><!-- imhCmpix_-42.png --><a href=\"\/support\/images\/stories\/wordpress-development\/functions\/imhCmpix_-42.png\" rel=\"lightbox-0\"><img decoding=\"async\" class=\"std_ss\" style=\"float: right; margin: 0px 15px 15px 15px; width: 200px;\" src=\"\/support\/images\/stories\/wordpress-development\/functions\/imhCmpix_-42.png\" alt=\"find header.php file\"><\/a>Now open the <code>header.php<\/code> file by highlighting it and clicking the <b>Edit<\/b> button or using a right-click and choosing <b>Edit<\/b> (depending on your theme, the <em>header.php<\/em> file may be in a \u201ctemplate parts\u201d directory\n<div style=\"clear: both;\"><\/div>\n<\/li>\n<li><!-- imhCmpix_-43.png --><a href=\"\/support\/images\/stories\/wordpress-development\/functions\/imhCmpix_-43.png\" rel=\"lightbox-0\"><img decoding=\"async\" class=\"std_ss\" style=\"float: right; margin: 0px 15px 15px 15px; width: 200px;\" src=\"\/support\/images\/stories\/wordpress-development\/functions\/imhCmpix_-43.png\" alt=\"add code to header.php file\"><\/a>Add the following code just beneath the <code>&lt;head&gt;<\/code> section:\n<pre class=\"code_block\">&lt;?php if ( ! is_preview() ) { \ninclude \"analytics-code.txt\"; \n} ?&gt;<\/pre>\n<div style=\"clear: both;\"><\/div>\n<\/li>\n<\/ol>\n<p>You may notice the exclamation point placed before the <em>is_preview()<\/em> function in the code snippet above. This means, if the page is <em>not<\/em> a preview page then insert our analytics code. This means all pages and posts will include the analytics code necessary for tracking purposes.<\/p>\n<p>Well done! You now know how to use the <strong>is_preview() function<\/strong> in a conditional statement to modify your preview page in WordPress.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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<a class=\"moretag\" href=\"https:\/\/www.inmotionhosting.com\/support\/edu\/wordpress\/the-is-preview-function\/\"> Read More ><\/a><\/p>\n","protected":false},"author":17,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[56],"tags":[],"class_list":["post-4236","post","type-post","status-publish","format-standard","hentry","category-wordpress"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.1.1 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>How to Use the is_preview() Function | InMotion Hosting<\/title>\n<meta name=\"description\" content=\"Worried that you are picking up pageviews from preview pages? In this article, we introduce the WordPress is_preview() function &amp; how to use it to avoid that.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.inmotionhosting.com\/support\/edu\/wordpress\/the-is-preview-function\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to Use the is_preview() Function | InMotion Hosting\" \/>\n<meta property=\"og:description\" content=\"Worried that you are picking up pageviews from preview pages? In this article, we introduce the WordPress is_preview() function &amp; how to use it to avoid that.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.inmotionhosting.com\/support\/edu\/wordpress\/the-is-preview-function\/\" \/>\n<meta property=\"og:site_name\" content=\"InMotion Hosting Support Center\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/inmotionhosting\/\" \/>\n<meta property=\"article:published_time\" content=\"2017-11-08T22:23:10+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-06-09T17:21:12+00:00\" \/>\n<meta name=\"author\" content=\"Christopher Maiorana\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@InMotionHosting\" \/>\n<meta name=\"twitter:site\" content=\"@InMotionHosting\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Christopher Maiorana\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/edu\/wordpress\/the-is-preview-function\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/edu\/wordpress\/the-is-preview-function\/\"},\"author\":{\"name\":\"Christopher Maiorana\",\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/#\/schema\/person\/c6922c56c84e17079fd558e07b7ef72f\"},\"headline\":\"How to Use the is_preview() Function\",\"datePublished\":\"2017-11-08T22:23:10+00:00\",\"dateModified\":\"2023-06-09T17:21:12+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/edu\/wordpress\/the-is-preview-function\/\"},\"wordCount\":389,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/#organization\"},\"articleSection\":[\"WordPress Tutorials\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.inmotionhosting.com\/support\/edu\/wordpress\/the-is-preview-function\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/edu\/wordpress\/the-is-preview-function\/\",\"url\":\"https:\/\/www.inmotionhosting.com\/support\/edu\/wordpress\/the-is-preview-function\/\",\"name\":\"How to Use the is_preview() Function | InMotion Hosting\",\"isPartOf\":{\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/#website\"},\"datePublished\":\"2017-11-08T22:23:10+00:00\",\"dateModified\":\"2023-06-09T17:21:12+00:00\",\"description\":\"Worried that you are picking up pageviews from preview pages? In this article, we introduce the WordPress is_preview() function & how to use it to avoid that.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/edu\/wordpress\/the-is-preview-function\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.inmotionhosting.com\/support\/edu\/wordpress\/the-is-preview-function\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/edu\/wordpress\/the-is-preview-function\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.inmotionhosting.com\/support\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to Use the is_preview() Function\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/#website\",\"url\":\"https:\/\/www.inmotionhosting.com\/support\/\",\"name\":\"InMotion Hosting Support Center\",\"description\":\"Web Hosting Support &amp; Tutorials\",\"publisher\":{\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.inmotionhosting.com\/support\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/#organization\",\"name\":\"InMotion Hosting\",\"url\":\"https:\/\/www.inmotionhosting.com\/support\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.inmotionhosting.com\/support\/wp-content\/uploads\/2023\/02\/inmotion-hosting-logo-yoast.jpg\",\"contentUrl\":\"https:\/\/www.inmotionhosting.com\/support\/wp-content\/uploads\/2023\/02\/inmotion-hosting-logo-yoast.jpg\",\"width\":696,\"height\":696,\"caption\":\"InMotion Hosting\"},\"image\":{\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/inmotionhosting\/\",\"https:\/\/x.com\/InMotionHosting\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/#\/schema\/person\/c6922c56c84e17079fd558e07b7ef72f\",\"name\":\"Christopher Maiorana\",\"description\":\"Christopher Maiorana joined the InMotion community team in 2015 and regularly dispenses tips and tricks in the Support Center, Community Q&A, and the InMotion Hosting Blog.\",\"sameAs\":[\"https:\/\/www.linkedin.com\/in\/chris-m-4623144b\/\"],\"url\":\"https:\/\/www.inmotionhosting.com\/support\/author\/christopherm\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to Use the is_preview() Function | InMotion Hosting","description":"Worried that you are picking up pageviews from preview pages? In this article, we introduce the WordPress is_preview() function & how to use it to avoid that.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.inmotionhosting.com\/support\/edu\/wordpress\/the-is-preview-function\/","og_locale":"en_US","og_type":"article","og_title":"How to Use the is_preview() Function | InMotion Hosting","og_description":"Worried that you are picking up pageviews from preview pages? In this article, we introduce the WordPress is_preview() function & how to use it to avoid that.","og_url":"https:\/\/www.inmotionhosting.com\/support\/edu\/wordpress\/the-is-preview-function\/","og_site_name":"InMotion Hosting Support Center","article_publisher":"https:\/\/www.facebook.com\/inmotionhosting\/","article_published_time":"2017-11-08T22:23:10+00:00","article_modified_time":"2023-06-09T17:21:12+00:00","author":"Christopher Maiorana","twitter_card":"summary_large_image","twitter_creator":"@InMotionHosting","twitter_site":"@InMotionHosting","twitter_misc":{"Written by":"Christopher Maiorana","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.inmotionhosting.com\/support\/edu\/wordpress\/the-is-preview-function\/#article","isPartOf":{"@id":"https:\/\/www.inmotionhosting.com\/support\/edu\/wordpress\/the-is-preview-function\/"},"author":{"name":"Christopher Maiorana","@id":"https:\/\/www.inmotionhosting.com\/support\/#\/schema\/person\/c6922c56c84e17079fd558e07b7ef72f"},"headline":"How to Use the is_preview() Function","datePublished":"2017-11-08T22:23:10+00:00","dateModified":"2023-06-09T17:21:12+00:00","mainEntityOfPage":{"@id":"https:\/\/www.inmotionhosting.com\/support\/edu\/wordpress\/the-is-preview-function\/"},"wordCount":389,"commentCount":0,"publisher":{"@id":"https:\/\/www.inmotionhosting.com\/support\/#organization"},"articleSection":["WordPress Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.inmotionhosting.com\/support\/edu\/wordpress\/the-is-preview-function\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.inmotionhosting.com\/support\/edu\/wordpress\/the-is-preview-function\/","url":"https:\/\/www.inmotionhosting.com\/support\/edu\/wordpress\/the-is-preview-function\/","name":"How to Use the is_preview() Function | InMotion Hosting","isPartOf":{"@id":"https:\/\/www.inmotionhosting.com\/support\/#website"},"datePublished":"2017-11-08T22:23:10+00:00","dateModified":"2023-06-09T17:21:12+00:00","description":"Worried that you are picking up pageviews from preview pages? In this article, we introduce the WordPress is_preview() function & how to use it to avoid that.","breadcrumb":{"@id":"https:\/\/www.inmotionhosting.com\/support\/edu\/wordpress\/the-is-preview-function\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.inmotionhosting.com\/support\/edu\/wordpress\/the-is-preview-function\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.inmotionhosting.com\/support\/edu\/wordpress\/the-is-preview-function\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.inmotionhosting.com\/support\/"},{"@type":"ListItem","position":2,"name":"How to Use the is_preview() Function"}]},{"@type":"WebSite","@id":"https:\/\/www.inmotionhosting.com\/support\/#website","url":"https:\/\/www.inmotionhosting.com\/support\/","name":"InMotion Hosting Support Center","description":"Web Hosting Support &amp; Tutorials","publisher":{"@id":"https:\/\/www.inmotionhosting.com\/support\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.inmotionhosting.com\/support\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.inmotionhosting.com\/support\/#organization","name":"InMotion Hosting","url":"https:\/\/www.inmotionhosting.com\/support\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.inmotionhosting.com\/support\/#\/schema\/logo\/image\/","url":"https:\/\/www.inmotionhosting.com\/support\/wp-content\/uploads\/2023\/02\/inmotion-hosting-logo-yoast.jpg","contentUrl":"https:\/\/www.inmotionhosting.com\/support\/wp-content\/uploads\/2023\/02\/inmotion-hosting-logo-yoast.jpg","width":696,"height":696,"caption":"InMotion Hosting"},"image":{"@id":"https:\/\/www.inmotionhosting.com\/support\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/inmotionhosting\/","https:\/\/x.com\/InMotionHosting"]},{"@type":"Person","@id":"https:\/\/www.inmotionhosting.com\/support\/#\/schema\/person\/c6922c56c84e17079fd558e07b7ef72f","name":"Christopher Maiorana","description":"Christopher Maiorana joined the InMotion community team in 2015 and regularly dispenses tips and tricks in the Support Center, Community Q&A, and the InMotion Hosting Blog.","sameAs":["https:\/\/www.linkedin.com\/in\/chris-m-4623144b\/"],"url":"https:\/\/www.inmotionhosting.com\/support\/author\/christopherm\/"}]}},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"primary_category":null,"_links":{"self":[{"href":"https:\/\/www.inmotionhosting.com\/support\/wp-json\/wp\/v2\/posts\/4236","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.inmotionhosting.com\/support\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.inmotionhosting.com\/support\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.inmotionhosting.com\/support\/wp-json\/wp\/v2\/users\/17"}],"replies":[{"embeddable":true,"href":"https:\/\/www.inmotionhosting.com\/support\/wp-json\/wp\/v2\/comments?post=4236"}],"version-history":[{"count":8,"href":"https:\/\/www.inmotionhosting.com\/support\/wp-json\/wp\/v2\/posts\/4236\/revisions"}],"predecessor-version":[{"id":105046,"href":"https:\/\/www.inmotionhosting.com\/support\/wp-json\/wp\/v2\/posts\/4236\/revisions\/105046"}],"wp:attachment":[{"href":"https:\/\/www.inmotionhosting.com\/support\/wp-json\/wp\/v2\/media?parent=4236"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.inmotionhosting.com\/support\/wp-json\/wp\/v2\/categories?post=4236"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.inmotionhosting.com\/support\/wp-json\/wp\/v2\/tags?post=4236"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}