Support » Fixing WordPress » Functions.php in a Child Theme

  • I’m using the Twenty Eleven theme and I am trying to alter the functions of my website. I have followed the very basic example in the Codex on how to use the functions.php file in a child theme. It is designed to add a favicon.

    It works, in that it adds the favicon but it breaks other aspects of the site: after I had created the functions.php file in the child theme as directed, I went into the admin side of the site and unticked “email me whenever anyone posts a comment”. But when I went to save the settings i got this error:

    Warning: Cannot modify header information - headers already sent by (output started at /.../wp-content/themes/geeks/functions.php:11) in /.../wp-includes/pluggable.php on line 934

    I am not sure why this happens but it seems to be confused between the original functions.php and the new one I added to the child theme. Do I need to add more code to the child function.php to get it to return to the original function.php or something?

    Looking at the instructions in the Codex at the link given above, it says:

    An opening PHP tag at the top, a closing PHP tag at the bottom, and, between them, your bits of PHP.

    However, looking at the twenty eleven functions.php file it can be seen that there is no such closing php tag. So is a closing php tag required or not? If it is, then why does the default functions.php file not have one?

Viewing 15 replies - 1 through 15 (of 17 total)
  • I have found the answer in another thread. It turns out that the Codex is actually wrong!

    It is misleading when it states that a closing php tag needs to be added. Adding the end tag will destroy your website and should NOT be added.

    Geez, I wonder how many other errors there are in the Codex???

    The Codex isn’t wrong. There should be closing PHP tag at the end of the file. Furthermore, that closing tag has to be the very last line of the file. If you have any blank lines after the closing tag, that can break things, too.

    A closing ?> tag at the end of a file isn’t actually needed and, in some cases. can stop errors occurring due to trailing white space or other characters after the closing tag.

    Thanks for clarifying. I guess it’s not actually needed, but just the recommended way to do it, as pretty much every PHP tutorial I’ve seen would tell you. Anyway, I’m sure we agree that the Codex isn’t wrong to say this, and most likely the OP had something after the closing tag that was causing the error.

    The point is that the Twenty Eleven theme itself doesn’t abide by the rule as described in the Codex.

    Why does the Codex tell us the closing php tag is required when the theme itself shows it is obviously not the case?

    Because the codex is written by anyone, not by WordPress

    It is a living document, editable by any volunteer. So it’s there because somebody put it there, not because it’s a WP standard

    Thanks Rev. Voodoo.

    If anyone can edit the Codex then it most likely will contain misleading information…such as this. If the Codex doesn’t abide by the standards, that is, if anyone can add anything to the Codex, then it is likely to contain many more instructions that don’t follow standards.

    That is misleading to the WP user trying to learn from the Codex.

    Hopefully somebody else would fix such things.

    Nobody gets paid to do this. The only way we can have a codex is to let volunteers populate it.

    That’s how a wiki works! While there may be some tradeoffs, it’s a great way to get people together to make documentation. Nobody is going to get paid to make this thing, and nobody is going to volunteer to write the whole darned thing. So people fill in what they know. If something is wrong, it gets edited.

    If you look at the huge amount of info in there, you can see it’s working out pretty well!

    Hopefully. So, has anyone corrected this misleading information in the Codex yet?

    I know what a Wiki is, but I was not aware that the Codex is a Wiki. Sure, my ignorance of that fact means little, but I wonder how many others thought like I did, that the Codex contains reliable standards for using WordPress.

    Please feel free to correct it yourself.

    Dave, I still disagree with you that the Codex is actually wrong here. At best, you could say that it is presenting one (commonly) recommended way of doing things.

    A closing PHP tag, while perhaps not completely necessary, is pretty much universally suggested. In strict programming terms, an opening tag should always have a matching closing tag. PHP is not always so strict, and leaving off the closing tag will probably still work in most cases, but a lot of programmers, such as myself, just don’t like the idea of having unbalanced tags.

    Also, FWIW, a closing PHP tag used correctly should never break your site. I have a Twenty Eleven child theme working with a closing tag in my functions.php file (as I do on all of my functions.php files). Did you already check that you had no white space after your closing tag?

    Thanks Jason, what you say makes sense. It’s probably more correct to say that Twenty Eleven adheres less to the standards than the Codex does.

    I think you are right, I did have white space before the closing tag and that is what broke the site, not the closing tag itself.

    It would be good for the Twenty Eleven developers to understand that users are expecting the default WordPress theme to set an example of good coding practices.

    It would be good for the Twenty Eleven developers to understand that users are expecting the default WordPress theme to set an example of good coding practices.

    And in many folks opinions, leaving the closing tag off IS good coding practices.

    And in many folks opinions, leaving the closing tag off IS good coding practices.

    I’m definitely in that group.

    Thanks for those links Rev.

    It seems then that this issue is not all that black and white and is not correct to say one way is right and the other is wrong. It’s more a matter of opinion on which is the best method.

    Thanks for everyone’s input, I’m slowly learning.

Viewing 15 replies - 1 through 15 (of 17 total)
  • The topic ‘Functions.php in a Child Theme’ is closed to new replies.