WordPress add html in header

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.

Читайте также:  Read input file line by line python

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.

The WPCode - Insert Headers, Footers, and Code Snippets plugin has a few sections, but the top is for adding code to the header. At the bottom it notes

  1. Install and activate the free WPCode – Insert Headers, Footers, and Code Snippets plugin.
  2. In the dark bar on the left-hand side of your dashboard, click Code SnippetsHeader & Footer.
  3. Paste the code into the Header box and click SaveChanges.

Common Services

Some popular services that require adding code to the site header include:

Источник

add code to WordPress header and footer

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.

Header and Footer WordPress plugin

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:

Install the plugin

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:

Читайте также:  Единицы измерения

Head, Footer and Post Injections interface

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:

Example of adding code to header

To add code to your site’s footer, you can scroll down to the BEFORE THE CLOSING TAG (FOOTER) option. Again, you have two options, though they’re different from the header section:

  • Desktop – despite the name, this adds code to the footer of both the desktop and mobile version of your site unless you specifically check the box for Mobile.
  • Mobilewhen checked, this lets you add a different code snippet to the mobile version of your site.

If you just want to add a code snippet to the footer section for all users, no matter their devices, paste it in the Desktop box and leave Mobile unchecked:

Example of adding code to footer

If needed, you can add multiple code snippets to each box.

Another neat thing about this plugin is that, if you’re using Google AMP for WordPress, the plugin lets you specifically add code snippets to the header and footer of the Google AMP versions of your pages (as long as you’re using the official AMP plugin from Automattic).

To do it, head to the AMP tab in Header and Footer and paste your code snippet into the appropriate box:

Google AMP interface

If you are simply needing to add different PHP functions on a site-wide basis, we also recommend checking out the free Code Snippets plugin. It removes the need to add custom snippets to your theme’s functions.php file. It currently has over 100,000 active installs with a 5 out of 5-star rating.

Code Snippets WordPress plugin

How to Manually Add Code to WordPress Header and Footer

If you’re not familiar with the basics of PHP, we recommend you stick with the plugin method above. The manual method might be overly complicated for you.

In this section, you’ll learn how to manually add code snippets to your theme’s header and footer via its functions.php file.

If you want to proceed with this method over the plugin in the previous section, it’s essential that you use a WordPress child theme to make your edits. If you don’t use a child theme, any code that you add to your header or footer will get overwritten if you update your WordPress theme.

Many developers provide a child theme. But if your developer doesn’t, here’s a guide on how to create a WordPress child theme. Once you have your child theme ready to go, you can proceed with the following steps to add code to your theme’s header or footer.

While you can add code snippets directly to your header.php and footer.php files, a better way is to use your functions.php file and the appropriate WordPress hook. This lets you keep all your snippets in one place and avoid modifying core theme files.

Читайте также:  Установит java на планшет

Step 1: Prepare Code Snippets

To get you started, we’ll give you a rough framework to add code to both your header and footer.

To add code to your header, use this code snippet:

/* Describe what the code snippet does so you can remember later on */ add_action('wp_head', 'your_function_name'); function your_function_name() < ?>PASTE HEADER CODE HERE ; 

To add code to your footer, use this code snippet:

/* Describe what the code snippet does so you can remember later on */ add_action('wp_footer', 'your_function_name'); function your_function_name() < ?>PASTE FOOTER CODE HERE ;

For each snippet, make sure to change:

  • The comment description (helpful when you need to remember what a code snippet does a year later)
  • The your_function_name placeholder (both instances)
  • The PASTE X CODE HERE placeholder

Step 2: Add Code Snippets to functions.php File in Child Theme

Once you have the relevant code snippet(s) ready, you need to add them to the functions.php file of your child theme. You can either edit this file by connecting to your site via FTP. Or, you can go to Appearance → Editor and select the functions.php file. Then, paste your code at the end of the file:

Add code to functions.php file

Make sure to save your changes and you’re done!

If you want more control over where your header or footer code snippets show up, you can use if statements to only add the code to specific pages on your WordPress site.

For example, to only add code snippets to the header or footer of your homepage, you could use:

/* Describe what the code snippet does so you can remember later on */ add_action('wp_head', 'your_function_name'); function your_function_name() < if(is_front_page()) < ?>PASTE HEADER CODE HERE >; 

Another option is to only add the code snippets to specific posts or pages. To do that, you can use this code snippet:

/* Describe what the code snippet does so you can remember later on */ add_action('wp_head', 'your_function_name'); function your_function_name() < if(is_single(73790)) < ?>PASTE HEADER CODE HERE >; 

Make sure to replace the example number – 73790 – with the actual ID of the post or page you want to add the code snippets to.

When In Doubt, Use The Plugin

That wraps up our guide on how to add code to the header or footer of your WordPress site. If the manual code examples are confusing, we recommend that you use the plugin method. It’s much more beginner-friendly and, most of the time, gives you just as much functionality.

Save time and costs, plus maximize site performance, with $275+ worth of enterprise-level integrations included in every Managed WordPress plan. This includes a high-performance CDN, DDoS protection, malware and hack mitigation, edge caching, and Google’s fastest CPU machines. Get started with no long-term contracts, assisted migrations, and a 30-day money-back guarantee.

Check out our plans or talk to sales to find the plan that’s right for you.

Get 4 Months Off

Enjoy 4 months of free hosting with an annual WordPress plan. 30-day money-back guarantee.

Источник

Оцените статью