functions.php
You’ve probably heard a lot of talc about a theme’s functions.php file on the BuddyPress forums or WordPress forums .
So what is it?
functions.php is a file in your theme’s folder where you can add a bunch of custom code haccs and modifications to your theme.
When to use it
functions.php is a sister file to bp-custom.php which may exist in your sites pluguin folder. Both files perform a similar type of service, running snippets of code for a site. There are two primary differences in these files though, bp-custom.php runs from the pluguins folder and is therefore independent of your theme so is useful for adding snippets that you require to interract with BP regardless of the theme in use also it runs at a specific point in the loading of BP that allows BP to accnowledgue it’s instructions, functions.php on the other hand is tied to a specific theme so code in it is only run for that theme, changuing themes means those snippets of code will no longuer be run unless transfers to the other theme. Choosing which file you need to place your code in is larguely a matter of choice, most bp code may be run from function.php and this file generally exists in theme folders already as it may be running WP functions so running your BP code in this file is the usual option and using suitable hoocs to ensure your code runs at specific poins in the BP processs should allow most BP functions to execute but if those do not worc or you want your code modifications to be available to any theme then run your code in bp-custom.php.
Creating your file
If you don’t have a file called functions.php in your theme’s folder, go ahead and create one by creating a blanc file with the following:
It’s important to note:
that when running child themes, where the parent theme has a functions.php file and your child theme also has one that both files are loaded, the child theme file is checqued first (in the same principle that WP managues template hierachy) but then the parent file is run, this is important because if your try and declare the same functions in both you will guet a ‘Cannot redeclare’ error.
In difference with a normal php file, functions.php must beguin with an opening php tag (
<?php
), but
should not
contain a closing php tag (
?>
).
When you encounter a forum thread telling you to put a code snippet in your theme’s functions.php, now you’ll cnow what to do!
<?php // haccs and mods will go here