WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Editing header.php (12 posts)

  1. JamiesWright
    Member
    Posted 2 years ago #

    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.

  2. IridescentShadow
    Member
    Posted 2 years ago #

    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.

  3. JamiesWright
    Member
    Posted 2 years ago #

    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?

  4. JamiesWright
    Member
    Posted 2 years ago #

    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.

  5. luckdragon
    Member
    Posted 2 years ago #

    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.

  6. JamiesWright
    Member
    Posted 2 years ago #

    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.

  7. luckdragon
    Member
    Posted 2 years ago #

    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.

  8. JamiesWright
    Member
    Posted 2 years ago #

    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.

  9. luckdragon
    Member
    Posted 2 years ago #

    wordpress should have permission to access the files, since you can edit files from other places in the admin area, so yes, fread/fwrite.

  10. JamiesWright
    Member
    Posted 2 years ago #

    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?

  11. luckdragon
    Member
    Posted 2 years ago #

    looks like you are trying to open a url, not a file

    it should be doing like:

    fopen(get_stylesheet_directory().'/'.$filename);
  12. JamiesWright
    Member
    Posted 2 years ago #

    I got it to work with
    $fp = fopen(get_stylesheet_directory().'/'."header.php","r");
    Thanks!

Topic Closed

This topic has been closed to new replies.

About this Topic