How to Add WordPress Header Code Without a Plugin

To add WordPress header code without a plugin, you can make changes to the header.php or footer.php files, though it is highly recommended to make changes via the functions.php file. Additionally, if you are not confident with code changes, it is better to use a plugin like WPCode – Insert Headers and Footers + Custom Code Snippets.

If you want to add code to the footer, you can follow the tutorial on this page: How to Add WordPress Footer Code Without a Plugin (Latest)

this article only discusses how to add code to the WordPress header without a plugin, and the file to be edited is the functions.php file

Requirements for Adding WordPress Header Code Without a Plugin

  • A website using the WordPress CMS
  • Access to the WordPress dashboard, or access to file manager/FTP/SFTP
  • Using a child theme (alternative)
  • Having the code or text that will be added to the header

How to Add WordPress Header Code Without a Plugin

Actually, we have several options for adding WordPress header code without a plugin, but here we will discuss how to add header code using only two options, namely through the WordPress dashboard and through the web hosting File Manager.

Option 1: Via the WordPress Dashboard

To add WordPress header code without a plugin via the WordPress dashboard page, first we need to log in to the WordPress dashboard page, then open the page Appearance → Theme File Editor.

Cara Menambahkan Kode di Header WordPress Tanpa Plugin

After being on the Theme File Editor page, we will get a notification that can be read and understood first, then click the button I Understand.

If done, then we can proceed to open the functions.php file that is in the theme we are editing.

Cara Menambahkan Kode di Header WordPress Tanpa Plugin

Add the following code into the functions.php file at the very last line to add custom code

/* Tulis deskripsi kode supaya mudah dicari ketika pengeditan berikutnya*/add_action('wp_head', 'nama_fungsi');function nama_fungsi(){?>TEMPEL KODE KALIAN DISINI<?php};
  • Make sure to change function_name as you wish
  • Change the comment so it can be found if you want to edit the file at another time
  • paste your code in the area PASTE YOUR CODE HERE

After the code is added, we can save the changes and make sure to clear the cache so the latest code can be read, because usually if you do not clear the cache, the code we just added will not run when we access the website.

Cara Menambahkan Kode di Header WordPress Tanpa Plugin

Option 2: Via File Manager

To add custom code via File Manager, we can first log in to our hosting dashboard page, for example cPanel, Plesk, Webuzo, or other web hosting control panels.

After being on the hosting dashboard page, open the file manager page and look for our WordPress installation folder, which is usually inside the folder public_html or www.

Next, from the WordPress installation folder, look for the folder named wp-content/themes and look for our active theme name, for example NEVE.

From within that active theme folder, we can find and edit the file functions.php, then add the following line of code at the very end of the functions.php file

/* Tulis deskripsi kode supaya mudah dicari ketika pengeditan berikutnya*/add_action('wp_head', 'nama_fungsi');function nama_fungsi(){?>TEMPEL KODE KALIAN DISINI<?php};

After it is added, we can save the file, then open WordPress again to clear the cache so the changes are read.

Adding WordPress Header Code Without a Plugin to Specific Pages

While the two codes above are used to add code to all WordPress pages, what if we want to add header code only on certain pages?

For example, the code is only displayed on the homepage, certain articles, or pages from a custom post type.

For that, the method is the same, but we need to use different code. Here are some codes that can be used on specific pages

Adding Header Code Only on the Homepage

/* Tulis deskripsi kode supaya mudah dicari ketika pengeditan berikutnya*/add_action('wp_head', 'nama_fungsi');function nama_fungsi(){if(is_front_page()) {  ?>TEMPEL KODE KALIAN DISINI<?php  }};

Adding Header Code on Specific Pages or Posts

/* Tulis deskripsi kode supaya mudah dicari ketika pengeditan berikutnya*/add_action('wp_head', 'nama_fungsi');function nama_fungsi(){if(is_single(67243,72433)) {  ?>TEMPEL KODE KALIAN DISINI<?php};

Change 67243,72433 to your Post or Page ID

Adding Header Code to Custom Post Types

/* Tulis deskripsi kode supaya mudah dicari ketika pengeditan berikutnya*/add_action('wp_head', 'nama_fungsi');function nama_fungsi(){if(is_singular(67243,72433)) {  ?>TEMPEL KODE KALIAN DISINI<?php};

Change 67243,72433 to the ID of the custom post type that has been published.

Written by

Muhammad Khoirun Nasikin

I enjoy tinkering with WordPress, hosting, VPS, web development, and various small experiments related to websites. I have over 3 years of experience in the web hosting industry.

COMMENTS

Loading...