Title: Editing header.php
Last modified: August 20, 2016

---

# Editing header.php

 *  Resolved [JamiesWright](https://wordpress.org/support/users/jamieswright/)
 * (@jamieswright)
 * [14 years ago](https://wordpress.org/support/topic/editing-headerphp/)
 * Hi,
    I would like to know what’s the best way to edit a header file with a plugin?
   The ultimate goal is to get the header file, find a certain word based on the
   theme, and insert a string after the word located. I have no clue where to begin,
   so any references or help is very much appreciated. Thanks.

Viewing 11 replies - 1 through 11 (of 11 total)

 *  [IridescentShadow](https://wordpress.org/support/users/iridescentshadow/)
 * (@iridescentshadow)
 * [14 years ago](https://wordpress.org/support/topic/editing-headerphp/#post-2759562)
 * You can read the entire file into a PHP string using the fopen() and fread() 
   PHP functions like so:
 *     ```
       $fp = fopen(get_template_directory_uri()."/header.php","r");
       $header_contents = fread($fp,filesize(get_template_directory_uri()."/header.php"));
       fclose($fp);
       ```
   
 * Then use the str_replace PHP function to find and alter your target word:
 * `$modified_header = str_replace("what you are looking for","what you are looking
   for"."what you want to add after it",$header_contents);`
 * Then save the modified contents back to the header.php file using the fopen()
   and fwrite() PHP functions:
 *     ```
       $fp = fopen(get_template_directory_uri()."/header.php","w");
       fwrite($fp,$modified_header);
       fclose($fp);
       ```
   
 * You’ll need to make sure the user your web server or PHP runs under has write
   access to header.php, but be mindful that making it writable for this process
   means it is writable to ANY PHP script on your server and could potentially be
   exploited.
 *  Thread Starter [JamiesWright](https://wordpress.org/support/users/jamieswright/)
 * (@jamieswright)
 * [14 years ago](https://wordpress.org/support/topic/editing-headerphp/#post-2759645)
 * Thanks for your input. I have tried the code but it gives me this error
 * ‘Warning: filesize() [function.filesize]: stat failed
    Warning: fread() [function.
   fread]: Length parameter must be greater than 0′
 * I’m assuming that the directory is not right, or the permission is not set correctly.
   I’m not sure what is the default setting for writing in the theme files. I’m 
   hoping that the plugin I’m writing should be able to edit the theme files without
   any permissions to be set, but I’m not sure if that’s possible.
 * I think the code would go something like:
    if get_current_theme() == ‘Twenty-
   Eleven’ Find </hgroup> Insert <?php theFunction() ?> after </hgroup>
 * What would you think is the best way to achieve this?
 *  Thread Starter [JamiesWright](https://wordpress.org/support/users/jamieswright/)
 * (@jamieswright)
 * [14 years ago](https://wordpress.org/support/topic/editing-headerphp/#post-2759647)
 * I’m thinking if there would be any WordPress action or filter to achieve this
   as well? I have gone through quite a bit of research, but have not found anything
   related to such a topic which is why I think I may be going in the wrong direction.
 * I understand that it will be easier if I was creating a theme option, which will
   enable me to add functions more easily. But I’m hoping to achieve that with a
   plugin, that has no control of the theme files. All the plugin will do is check
   for the user’s theme, and add the relevant code in the respective places. Thanks.
 *  [luckdragon](https://wordpress.org/support/users/luckdragon/)
 * (@luckdragon)
 * [14 years ago](https://wordpress.org/support/topic/editing-headerphp/#post-2759648)
 * the problem with your idea is that themes are so different, some use the standard
   wp stuff, some use Themator, some use gantry, etc… so trying to find the code
   would be difficult depending upon the theme.
 *  Thread Starter [JamiesWright](https://wordpress.org/support/users/jamieswright/)
 * (@jamieswright)
 * [14 years ago](https://wordpress.org/support/topic/editing-headerphp/#post-2759649)
 * yes, I’ve considered that aspect, but I figured I would do it for the more popular
   themes and see how it goes. but I’ll first have to find out the way to edit the
   theme files directly from the plugin, which I think should be possible as there
   are WordPress filters to change the header image etc. I’m wondering if there 
   are any actions/ filters for this, or do I have to get the theme file from the
   plugin, edit, and replace the original file.
 *  [luckdragon](https://wordpress.org/support/users/luckdragon/)
 * (@luckdragon)
 * [14 years ago](https://wordpress.org/support/topic/editing-headerphp/#post-2759650)
 * all you have to do is read the template, then parse through it, put in your code,
   and then write it back to itself, you might want to move/rename the original 
   to like templatename.php.orig or something similar, just in case something breaks.
 *  Thread Starter [JamiesWright](https://wordpress.org/support/users/jamieswright/)
 * (@jamieswright)
 * [14 years ago](https://wordpress.org/support/topic/editing-headerphp/#post-2759651)
 * thanks. but I’m still not quite sure how I would achieve that. would I be using
   fopen and fread as suggested by **IridescentShadow** above? would I encounter
   permission error by default if I use this approach? I’m only interested in editing
   the header.php file at this moment.
 *  [luckdragon](https://wordpress.org/support/users/luckdragon/)
 * (@luckdragon)
 * [14 years ago](https://wordpress.org/support/topic/editing-headerphp/#post-2759652)
 * wordpress should have permission to access the files, since you can edit files
   from other places in the admin area, so yes, fread/fwrite.
 *  Thread Starter [JamiesWright](https://wordpress.org/support/users/jamieswright/)
 * (@jamieswright)
 * [14 years ago](https://wordpress.org/support/topic/editing-headerphp/#post-2759653)
 * Hi,
    I’m getting this error from the code:
 *     ```
       Warning: filesize() [function.filesize]: stat failed for http://localhost:8888/wordpress/wp-content/themes/twentyeleven/header.php
       Warning: fread() [function.fread]: Length parameter must be greater than 0
       Warning: fopen(http://localhost:8888/wordpress/wp-content/themes/twentyeleven/header.php) [function.fopen]: failed to open stream: HTTP wrapper does not support writeable connections
       Warning: fwrite() expects parameter 1 to be resource, boolean given
       Warning: fclose() expects parameter 1 to be resource, boolean given
       ```
   
 * From what I’ve read, the first 2 errors could be the cause of the directory not
   being writeable. I’m using MAMP for hosting, is this because I’m working on this
   locally?
 *  [luckdragon](https://wordpress.org/support/users/luckdragon/)
 * (@luckdragon)
 * [14 years ago](https://wordpress.org/support/topic/editing-headerphp/#post-2759654)
 * looks like you are trying to open a url, not a file
 * it should be doing like:
 *     ```
       fopen(get_stylesheet_directory().'/'.$filename);
       ```
   
 *  Thread Starter [JamiesWright](https://wordpress.org/support/users/jamieswright/)
 * (@jamieswright)
 * [14 years ago](https://wordpress.org/support/topic/editing-headerphp/#post-2759669)
 * I got it to work with
    `$fp = fopen(get_stylesheet_directory().'/'."header.php","
   r");` Thanks!

Viewing 11 replies - 1 through 11 (of 11 total)

The topic ‘Editing header.php’ is closed to new replies.

## Tags

 * [header](https://wordpress.org/support/topic-tag/header/)
 * [javascript](https://wordpress.org/support/topic-tag/javascript/)

 * In: [Hacks](https://wordpress.org/support/forum/plugins-and-hacks/hacks/)
 * 11 replies
 * 3 participants
 * Last reply from: [JamiesWright](https://wordpress.org/support/users/jamieswright/)
 * Last activity: [14 years ago](https://wordpress.org/support/topic/editing-headerphp/#post-2759669)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
