I really appreciate everyone on this site who takes the time to answer questions here. I've learned a good deal already reading, but am sure I could learn even more if I could get past at least one basic question.

Just like the "Best Collection of code for your functions.php file" post on this site, which has some outstanding tips, there are many great ideas for development without weighting things down with plugins.

Trouble for me is, I clearly don't know enough about what I'm doing because anytime I've tried to add code to the functions.php file...I break my site. I am clearly placing it incorrectly and there is probably a step missing in the process as well.

I obviously know just enough to be dangerous.

I've done a lot of googling with variations on "how to properly edit functions.php" but I can't find a "101" version anywhere.

Any thoughts on this would be appreciated, thanks!

  • It would help if we could see an example of your functions.php file after it's "broken" so we can tell you what to change. Can you drop an example in a pastebin? – EAMann Dec 23 '10 at 15:41
  • @EAMann Thank you for taking the time to look at my question. When I make my next attempt at this, I will save the code separately. So that my site didn't stay down, I copied over the functions.php from a copy of my theme that I had on hand just in case. – Shelli Dec 24 '10 at 7:19

First of all, make sure you try things out in a development environment on your own PC. This way you can try things out without breaking your live site. The XAMPP package contains everything you need to run it on your own Windows, Mac, Linux or Solaris machine.

Then, enable debugging mode by setting WP_DEBUG to true in your wp-config.php. You need this to display errors, otherwise you get the infamous "white screen of death" (basically WordPress ran into an error, but for security reasons doesn't show everyone what it is, leaving you to wonder what went wrong).

With this basic setup you can start playing with functions.php. Make sure you don't add an ending ?> tag: PHP doesn't need it, and you might inadvertently add a blank line after it, which will start the output and prevent sending HTTP headers later (for redirection or cookies, like on login). So your file starts with <?php, and then contains only PHP code.

It's also a good idea to disable all other plugins and use a basic theme. This way, you can be sure you are the only using the different hooks. Or if you use plugins, enable them one by one and test your site at each step, so you know what plugin causes an error. (Enabling WP_DEBUG might show you many notices indicating sloppy coding style, even in "official" plugins - these do not always lead to errors, but they don't improve your confidence in the code.)

After this you should follow the specific error messages you get. I also use the Xdebug PHP extension to get easy-readable traces of error messages, and I recommend you do it too.

  • Big plus on not adding the closing ?>; definitely makes life easier when you don't. – MikeSchinkel Dec 22 '10 at 7:44
  • +1, i usually avoid it for most files, it's easier to avoid blank spaces at the end of files, and that infamous headers already sent warning you can receive due to said blank space.. – t31os Dec 22 '10 at 8:56
  • Thank you for the reminder on the XAMPP. I actually do have that installed on my Mac and of course should be trying some of this stuff there. I was able to paste the original functions.php over the mistake I'd made. I really appreciate the tips, very much. I am confused about one thing. @Rarst said the basic construction of the php files requires the ?> - but that is incorrect? – Shelli Dec 24 '10 at 7:38
  • @Shelli: PHP does not require the ?>, but if you use it in functions.php, it should be at the bottom. Since it is optional, some find it easier to leave it off. Others prefer to include it, so you can verify you uploaded the complete file to the server. You can do both by writing a comment // END OF FILE at the end of course. – Jan Fabry Dec 24 '10 at 10:16

Do you have proper PHP tags in your functions.php file?

File starts.


// stuff goes here


File ends.

  • THANK YOU. I am sure this sounds really dumb, but that tip is exactly what I was looking for. Prior to this point, I have only really played around with the style.css file and then was inserting in the index.php. The instructions on inserting were very specific, such as "paste this at the bottom" or add just under "the time". None of the things I've read about insertion into the functions file mentions placement as I recall. Of course coders offering snippets or a very specific tutorial assume people know how to open and close. Again, thanks! – Shelli Dec 24 '10 at 7:22
  • @Shelli most WordPress tutorials assume that user has some PHP knowledge. I highly recommend to skim through PHP basics (it has most excellent documentation) so it doesn't get in the way when you are trying to tweak WP. :) – Rarst Dec 24 '10 at 7:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy