Child theme : how to insert code? (6 posts)

  1. Hi!

    So I'm trying to use child themes as strongly reccommended everywhere. If it's for modifying the CSS, no problem, got that covered. But what if I want to insert a single php command, or even simpler, a <span> in a specific spot in the header.php, for example? How can I do that in my child theme? By copy-pasting the whole Parent theme header.php into my child them directory, and editing it there? But what if the parent theme gets updated? I won't get the benefit of whatever is updated then.. So I don't quite get how to do it.

  2. Antanarive
    Posted 5 years ago #

    Here two examples of what I have done with a child theme for the Thematic Framework theme. The code below has been appended to the function.php file of the child theme.

    The first one: it customizes the "post header meta information" ("Published by: ... Date:..."):

    function my_postheader_postmeta(){
    	$postmeta = '<div class="entry-meta">';
    	// $postmeta .= thematic_postmeta_authorlink();
    	// $postmeta .= '<span class="meta-sep meta-sep-entry-date"> | </span>';
    	$postmeta .= thematic_postmeta_entrydate();
    	$postmeta .= thematic_postmeta_editlink();
    	$postmeta .= "</div><!-- .entry-meta -->\n";
    	return $postmeta;

    The seconde one customizes the footer of the whole website (sorry, in French):

    function my_footer($thm_footertext) {
    	$thm_footertext = 'Propulsé par
            <a href="http://www.wordpress.org">WordPress</a>.
            Construit avec le
            <a href="http://www.themeshaper.com/thematic">
            Thematic Theme framework</a> et mon propre thème dérivé:
            <a href="http://www.antanarive.com/blog/prodigal-son/">
            Prodigal Son</a>.';
            return $thm_footertext;
    add_filter('thematic_footertext', 'my_footer');

    How does it work? In both examples, you have to find the name of the function that you want to "override". You write your own version of it (removing or adding instructions), then you add a "add_filter" instruction to inform WordPress to take into account your function instead of the original one.

    If you want to add some HTML code, you'll just have to find the original PHP function that is generating the HTML for that area where you want to insert your additional code.

    Most real functions are not in the single.php, header.php, etc but in the content-extension.php, and others. A full text search on the files may help. You can also find some tutorials by googling CHILD THEME FUNCTION FILTER HOOK.. etc.. These kind of keywords.

    Good luck!

    As I am slowly building a child theme, I post all the changes I make here:

  3. Hi!

    Thanks for the detailed explanation, and I hope it'll be useful for someone else, but I was already aware of what you explained. I'm sorry I didn't make myself clear in my original question.

    What I want is the following; let's say my theme has this HTML (NOT php) code, in header.php for example (but it can be anywwhere, really):

    <div id="WebSite">
    <!-- Header Begin -->
    <div id="Header">
    <span class="Logo">[content]</div><!--end Logo-->

    In this simple example let's say for argument's sake I want to wrap my span with another one:

    <div id="WebSite">
    <!-- Header Begin -->
    <div id="Header">
    <span class="LogoWrap">
    <span class="Logo">[content]</div><!--end Logo-->
    </div><!--end LogoWrap-->

    How can I do something simple like this without touching the Parent theme's templates?

    Ok, I realize I can maybe do some jQuery magic to accomplish this (maybe? I'm not experienced enough yet with it), but that seems like a complicated way of doing this. And what if I wanted to do something even more complicated?

    So, I would love to do this kind of thing in a child theme without completely copying the Parent theme's template and modyfing it, but I just don't see how..

    I'm learning php bit by bit by fiddling with WP, so maybe there's some obvious way of doing it (in functions.php?) that I'm not seeing, so hopefully someone will elucidate for me!


    Oh, and Antanarive, French is fine, I'm from Quebec! :)

  4. Antanarive
    Posted 5 years ago #


    Sorry for the misunderstanding. A quick google search seems to indicate that you can just copy the header.php in your child theme folder, and modify it. It would then be taken into account automatically.

    This is what I understand when reading this:

    Copy header.php to your Child Theme folder and edit it there. In WP 2.6 you'll need to use WordPreciousss for WP to see it but 2.7 will recognize it automatically.

    I have never done it... but I'll be interested to hear the result of your testing... :-)

  5. Hi,

    Thanks, I'd heard of that too, but then what if header.php gets updated in the parent theme? I want to use the new header.php without losing my changes.. but I guess that it may be a contradictory request (because the updated header.php might have a completely different layout and thus my new code might where it used to anymore..).

  6. Antanarive
    Posted 5 years ago #

    Well.. You can't have all at the same time! :-)

    By using a separate file in your child theme, you ensure that
    1) you don't overwrite the current parent theme
    2) you don't overwrite your child theme / changes when updating the parent theme
    3) you can easily remove your child file if it seems incompatible with the updated parent child
    4) you can then take your time to rebuild your child file, mixing your changes with the new parent file...

    But yes, it will still require some work...

Topic Closed

This topic has been closed to new replies.

About this Topic


No tags yet.