WordPress.org

Ready to get started?Download WordPress

Forums

Functions File in a Child theme (11 posts)

  1. Context Canada
    Member
    Posted 2 years ago #

    I have not been able to execute any commands in my Child them functions file.
    I would like to cover some of the basics please:
    Must the Child theme functions file be renamed ie "it must be called something-functions.php'? There seems to be some confusion as to the best practice here!
    Since this is a Child of TwentyTen does the functions file have to begin with or include the following code?
    add_action( 'after_setup_theme', 'the_name_of_the_childtheme_setup' );

    function the_name_of_the_childtheme_setup() {

  2. Megan Goodson
    Member
    Posted 2 years ago #

    As far as I know all you need is a .php file called functions.php and have <?php at the beginning of the file and ?> at the end and then in between you put your functions.

    Hope this helps a little bit, but above is my experience with child-themes and functions.php

    Most of the time I copy my functions.php over from my main theme and edit it in the child. But I heard adding a new file called functions.php works as well. I think it's preference...

  3. dragonsjaw
    Member
    Posted 2 years ago #

    This is what one of my child themes of twentyten functions file looks like:
    [Code moderated as per the Forum Rules. Please use the pastebin]

    It works for me.

  4. esmi
    Forum Moderator
    Posted 2 years ago #

    Must the Child theme functions file be renamed ie "it must be called something-functions.php'?

    No. It needs to be called functions.php (just like the parent theme's file).

    does the functions file have to begin with or include the following code?

    No. That will be picked up via the parent's functions.php file. You only add new or amended functions to your child's functions.php file.

    and ?> at the end

    No - that is not needed. PHP will stop parsing automatically when it reaches the end of the file. Omitting the closing ?> tag is a good way to avoid "header already sent" warnings due to whitespace or other invisible characters after the closing ?> tag.

    Most of the time I copy my functions.php over from my main theme and edit it in the child.

    That is extremely bad practice and in some cases, can stop a theme in its tracks. You should only only ever add new or amended functions to your child's functions.php file.

  5. Context Canada
    Member
    Posted 2 years ago #

    Thanks esmi. I caught the reference to omitting the closing tag in one of your earlier posts. I found this forum link http://wordpress.org/support/topic/twentyten-child-functionphp-filter-header-image-height?replies=11 and finally something is working. I can modify the header size!
    However there seems to be a conflict here again with coding rules and using the endif argument.
    Is there something else that might be in play such as my htaccess?
    Note: I use these themes in a WPMU setting for testing and it just seems to be an awkward beginning to getting anywhere with modified functions.

  6. Context Canada
    Member
    Posted 2 years ago #

    So sorry. I should have mentioned that the code was entered exactly as posted and the file name is just functions.php and there is no need to change the name of the child_theme. So that is good.

  7. esmi
    Forum Moderator
    Posted 2 years ago #

    Try adding:

    function my_header_width($width) {
    	$width = xxx;
    	return $width;
    }
    function my_header_height($height) {
    	$height = yyy;
    	return $height;
    }
    add_filter( 'twentyten_header_image_width', 'my_header_width');
    add_filter( 'twentyten_header_image_height', 'my_header_height');

    to your child theme's functions.php file. Just change xxx and yyy to the desired header image dimensions.

  8. Dave333
    Member
    Posted 2 years ago #

    Thank you esmi,

    You wrote:

    No - that is not needed. PHP will stop parsing automatically when it reaches the end of the file. Omitting the closing ?> tag is a good way to avoid "header already sent" warnings due to whitespace or other invisible characters after the closing ?> tag.

    This had me stumped for hours. Mainly because I naively thought that the instructions given in the Codex were correct:

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

    Adding that closing php tag as instructed broke my website. So the Codex is just plain wrong when it says to add a closing php tag. How can we trust using the Codex when it give such bad instructions that break the site?

    How frustrating.

    Can the Codex be corrected now that we know it to be wrong?

  9. Chip Bennett
    Theme Review Admin
    Posted 2 years ago #

    Adding that closing php tag as instructed broke my website.

    Not likely. That behavior is indicative of some other issue, the most common being white space after the closing PHP tag. Another issue might be improperly written functions in functions.php, that are returning output improperly (though likely you'll still get the "headers already sent" error, just at a later point in the process).

    So the Codex is just plain wrong when it says to add a closing php tag.

    It's not wrong, it's just not an exclusive or exhaustive answer. As @esmi pointed out, there is nothing wrong with omitting the closing PHP tag in functions.php. Either adding it or omitting it is equally valid with respect to proper PHP syntax. Mainly, the difference is that omitting the closing PHP tag might be a bit more error-proof.

    How can we trust using the Codex when it give such bad instructions that break the site?

    The instructions aren't bad; they just may not be as complete or thorough as they could be. That said: anyone with valid wordpress.org login credentials can edit the Codex.

  10. Dave333
    Member
    Posted 2 years ago #

    Thanks for your response Chip.

    However, I removed the end tag from my Functions.php file and it works, the site is no longer broken.

    There were no new functions other than the one to add a favicon (as directed in the Codex) so it was not that. It was the end php tag that broke the site.

  11. Dave333
    Member
    Posted 2 years ago #

    Actually, I think you are 100% right Chip.

    After re-reading what you said about the white space after the closing tag I went back and added the end tag and checked again. The site works fine. So I went back and added some white space after the closing tag but the site still worked.

    So I am not not really sure what was the cause of the original fault.

    Thanks for your thorough answer.

Topic Closed

This topic has been closed to new replies.

About this Topic