WordPress.org

Ready to get started?Download WordPress

Forums

Functions.php in a Child Theme (18 posts)

  1. Dave333
    Member
    Posted 2 years ago #

    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.

    http://codex.wordpress.org/Child_Themes

    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?

  2. Dave333
    Member
    Posted 2 years ago #

    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???

  3. jason_lane
    Member
    Posted 2 years ago #

    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.

  4. esmi
    Forum Moderator
    Posted 2 years ago #

    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.

  5. jason_lane
    Member
    Posted 2 years ago #

    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.

  6. Dave333
    Member
    Posted 2 years ago #

    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?

  7. Rev. Voodoo
    Volunteer Moderator
    Posted 2 years ago #

    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

  8. Dave333
    Member
    Posted 2 years ago #

    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.

  9. Rev. Voodoo
    Volunteer Moderator
    Posted 2 years ago #

    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!

    http://en.wikipedia.org/wiki/Wiki

  10. Dave333
    Member
    Posted 2 years ago #

    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.

  11. esmi
    Forum Moderator
    Posted 2 years ago #

    Please feel free to correct it yourself.

  12. jason_lane
    Member
    Posted 2 years ago #

    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?

  13. Dave333
    Member
    Posted 2 years ago #

    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.

  14. Rev. Voodoo
    Volunteer Moderator
    Posted 2 years ago #

    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.

    http://framework.zend.com/manual/en/coding-standard.php-file-formatting.html

    http://stackoverflow.com/questions/4410704/php-closing-tag

    http://codeigniter.com/user_guide/general/styleguide.html#php_closing_tag

  15. esmi
    Forum Moderator
    Posted 2 years ago #

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

    I'm definitely in that group.

  16. Dave333
    Member
    Posted 2 years ago #

    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.

  17. nigedo
    Member
    Posted 2 years ago #

    Just adding thanks and +1 to Dave333 for provoking this discussion, and to everyone else for responding patiently. The missing closing tag in Twenty Eleven's functions.php was confusing me too.

  18. droimbai
    Member
    Posted 1 year ago #

    Thanks for this interesting discussion. I read that putting a PHP close tag incorrectly could pose security problems. Frankly, I'd never know about this until I stumbled upon this discussion and read a little more about it (I'm not a programmer, BTW).

    Then, my question is, should we leave out the closing tab for all PHP files in WordPress themes and plugins, or do we do it selectively? If the latter, what are the criteria we should use?

    Thank you!

Topic Closed

This topic has been closed to new replies.

About this Topic