Home » Php » php – WordPress- How to insert code to header for a specific page only?

php – WordPress- How to insert code to header for a specific page only?

Posted by: admin July 12, 2020 Leave a comment


I use wordpress and would like to add 2 lines of code to the header of one page only.

The problem is that header.php will change all the site’s headers and I want it to change only the header of one specific page.

The only thing I want to do is add this 1 line :

<META name="robots" content="noindex, nofollow"/>
How to&Answers:

If you want to have a different header for a certain page you need to download your header.php from FTP, rename it to header-new.php (replace “new” with whatever you want), re-upload header-new.php to the same directory as your original header.
– On the page template you want the new header to show up on


<?php get_header(); ?> 


<?php get_header('new'); ?>

and now your new header will show up only on that specific page template


you have to just add your pageid on your header file like this

global $post;
if($post->post_type == 'page' && $post->ID == 'whatever page id'){
   echo '<meta name="robots" content="noindex, nofollow" />';

it will just display meta on specific page which you want.

suppose we have add just pageid == ‘5’ it will display meta tag in just page id = ‘5’.


Just correcting the answer of FDL, use this:

global $post;
if($post->post_type == 'page' && $post->ID == 'yourid'){
   echo '<meta name="robots" content="noindex, nofollow" />';


Custom Fields are a great way to allow you to have page specific meta data that you can create logic on in your template files. The Codex link gives great examples of this.


<?php global $post; ?>
<?php if(is_page('pagename')){ ?>
      <meta name="robots" content="noindex, nofollow" />
<?php } ?>


There’s a plugin to do exactly the thing requested.
HiFi (Head Injection, Foot Injection)

There’s a “This plugin hasn’t been updated in over 2 years….” notification, but I’ve just tested it on WP 3.5.2 – it works.


An easier, non-coding solution to changing the robots meta tag on a per-page basis is by using the Yoast SEO plugin.
You can set individual pages (such as a form thank-you page) as noindex and even nofollow if you’re so inclined. If you’re using Yoast to generate your sitemap as well, then you can exclude that page from the sitemap at the same time you noindex it, which will prevent errors in Search Console.

If you want to have the ability to add some other tags or esoteric syntax to the then you can use the Per Page Add to Head plugin
Which will allow you to be very granular about which page gets which code.


This worked for me. I didn’t feel I needed to get the post type. I just needed the ID.

$post = get_post();
if ( $post->ID == 1234 ){
    echo '<!-- Point to the AMP version of this page. -->';
    echo '<link rel="amphtml" href="/amp/amp-story">';

This did not work for me as spec’d in the Codex (https://developer.wordpress.org/reference/functions/is_page/).

is_page( 1234 )


Try with this:

if (is_page('page_slug') { ?>
   <META name="robots" content="noindex, nofollow"/>
<?php }

You can check the reference here: https://developer.wordpress.org/reference/functions/is_page/


 <?php if (is_home()) { ?>
    <META name="robots" content="noindex, nofollow"/>
 <?php } ?>

this code will put meta only for home page, you may use is_single, is_archive, is_404 etc.