5

I setup a child theme which works fine. It's importing the main style.css and has custom css styles along with custom header.php and footer.php files

When using functions.php in a child theme it's going to append the child themes functions.php to the main theme. Do I need to wrap my functions in opening and closing php tags or can I just add the following without php tags add_image_size( 'related-thumbnail', 130, 130, true );

3 Answers 3

8

Opening tags are required, closing tags are only needed if you have other code (HTML, CSS) after them in the same file. See PHP manual.

A closing tag with nothing after it may lead to problems with included files if they contain a BOM. That’s one of the reasons why all optional closing tags were removed for WordPress 3.4 in core files. Follow this style. It’s useful.

Here is an example from Automattic’s _s theme’s functions.php.

1
  • So with no HTML or CSS after my add_image_size function in the child theme functions.php I don't have to close it? I'll be testing this later tonight and will probably select this as the answer. Thanks
    – Anagio
    Jul 26, 2012 at 11:41
6

The opening tag <?php is mandatory. The closing one ?>, not.
But it is recommended not have the closing tag at all, as it can break/freeze the site due to extra white spaces after it.

It's also recommended not to use the short opening tag <?, as some servers may not have it enabled.

Then you may have some Html being printed in the middle of PHP code, when the later is closed and reopened after the former ended.
Or vice versa, when the PHP file starts with Html, and the PHP will be opened-closed and Html follows.

Documentation (as usual there, the comments are worth reading too):
http://php.net/manual/en/language.basic-syntax.phpmode.php

In this Question's context, if the parent's functions.php has a closing tag, just check if no white spaces after it, or simply remove it.

And in the child's functions.php, just put the opening tag and start writing/pasting all the needed mods/enhancements. Taking care that extra <?php ?> are under control.

4
  • While writing this one, herr @toscho posted his... Prost, maestro :o)
    – brasofilo
    Jul 26, 2012 at 11:58
  • 1
    Wait … you needed 30 minutes for that? I write five plugins in 30 minutes. :P
    – fuxia
    Jul 26, 2012 at 11:59
  • While writing the first answer of the day, I need many coffee breaks to perform and the poor textbox is left open :o) . . .
    – brasofilo
    Jul 26, 2012 at 12:04
  • 1
    @toscho Well get you ;) I write my answers like my code - slow, steady & never considered finished xD Jul 26, 2012 at 12:07
0

Your php file should always begin with an opening <?php tag.

2
  • The functions.php file in the parent theme has the opening php tag, has plenty of functions but doesn't close at the end. So i'm figuring some place it's closing them from another file. So i'm wondering if my child theme functions.php should have any php tags since it's content is added to the parent theme functions.php
    – Anagio
    Jul 26, 2012 at 11:04
  • 1
    it is explained in the Codex: codex.wordpress.org/Child_Themes#Using_functions.php
    – Michael
    Jul 26, 2012 at 11:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.