- WordPress- How to insert code to header for a specific page only?
- 10 Answers 10
- Add Code to Headers
- Integrated Services
- Adding Code to Headers.php
- Common Services
- How to Add Code to WordPress Header and Footer
- How to Add Code to WordPress Header and Footer With a Plugin
- Step 1: Install and Activate Head, Footer and Post Injections
- Step 2: Add Code Snippet to Head, Footer and Post Injections
- BONUS – Add Code to Header and Footer of Google AMP Pages
- Step 1: Prepare Code Snippets
- Step 2: Add Code Snippets to functions.php File in Child Theme
- BONUS: Add Code to Header or Footer For Only Specific Pages
- When In Doubt, Use The Plugin
- Get 4 Months Off
WordPress- How to insert code to header for a specific page only?
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 :
The WP global variable $pagename should be available for you. $pagename is defined in the file wp-includes/theme.php , inside the function get_page_template() , which is of course called before your page theme files are parsed, so it is available at any point inside your templates for pages.
10 Answers 10
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
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 == page_int)< echo ''; >
it will just display meta on specific page which you want.
Suppose you only want to output the code for the page with set page_int to 5 . This is an integer, so do not use single quotes around it.
Just correcting the answer of FDL, use this:
global $post; if($post->post_type == 'page' && $post->ID == 'yourid')< echo ''; >
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.
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. https://yoast.com/wordpress/plugins/seo/ 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 https://wordpress.org/plugins/per-page-add-to/ 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.
Add Code to Headers
Some services, such as Google Adsense and Mailchimp, ask you to add code to the site header in order to verify the site, add content, integrate features, or more. This guide will show you how.
Integrated Services
For Google, Bing, Pinterest, Yandex, and Facebook Business, we have a more straightforward way to verify your site or domain, explained on Site Verification Services. This method allows you to safely add the code without manipulating site files.
Visit Tools → Marketing → Traffic and scroll down to Site verification services to access these tools.
Adding Code to Headers.php
This section of the guide applies to sites with the WordPress.com Business or Commerce plan. If your site has one of our legacy plans, this feature is available on the Pro plan.
If you’re trying to add code to the header of your site, the instructions may vary, but it can usually be done the same way here at WordPress.com. Some places will explain that it should be added between
and while some will suggest using a theme editor or adding to the HTML header.The reason these codes are added to the header is so that the code shows on every page and post of your site.
We recommend using a plugin to do this, such as the WPCode – Insert Headers, Footers, and Code Snippets plugin.
- Install and activate the free WPCode – Insert Headers, Footers, and Code Snippets plugin.
- In the dark bar on the left-hand side of your dashboard, click Code Snippets → Header & Footer.
- Paste the code into the Header box and click SaveChanges.
Common Services
Some popular services that require adding code to the site header include:
How to Add Code to WordPress Header and Footer
How to Add Code to WordPress Header and Footer With a Plugin
For most users, the easiest way to add code to the WordPress header and footer is via a plugin. The benefits of using a plugin over the manual method in the next section are:
- Your code snippets will remain intact if you ever switch themes.
- The plugin makes it easy to only add code snippets to the header or footer of your homepage, which can be helpful in some use cases.
- The plugin is more beginner-friendly because it doesn’t require digging into the code.
While there are several plugins that offer this functionality, we recommend the Head, Footer and Post Injections plugin from Stefano Lissa because it gives you more control than many of the alternatives. It currently has over 200,000 active installs with a 5 out of 5-star rating.
Step 1: Install and Activate Head, Footer and Post Injections
Head, Footer and Post Injections is available for free at WordPress.org, so you can install and activate it directly from your WordPress dashboard by going to Plugins → Add New and searching for it:
Step 2: Add Code Snippet to Head, Footer and Post Injections
Once you activate the plugin, you can access its interface by going to Settings → Header and Footer in your WordPress dashboard. You’ll see a number of tabs in the plugin’s interface. But for this guide, you’ll mostly work in the default Head and footer tab:
To add code to your site’s header, you need to paste it into one of the boxes under the SECTION INJECTION area:
- Every page – adds the code snippet to the header of every single page on your site. This is what you’ll want to use most of the time.
- Only home page – only adds the code snippet to the header of your homepage.
For example, to add the Google Analytics tracking code to WordPress, you’d just paste it into the Every page box and save your changes:
To add code to your site’s footer, you can scroll down to the BEFORE THE