WordPress has 100s of built in php functions called core functions.
However if you want to modify WordPress, create Plugins or themes, then you will probably need to create your own custom functions.
What You Will Learn
- How to Add Your Own Functions to WordPress
- How to Customise existing WordPress functions
The Functions.php File
The main functions.php file contains WordPress core php functions, and is located in the includes directory.
It is one of the first files loaded as part of the WordPress load process.
Although it can be customised just like any of the other WordPress files,customising the main functions.php isn’t recommended, as any changes will be overwritten when you do an upgrade.
Therefore if you need to add your own functions to WordPress or modify existing ones then you should:
- Use the functions.php in your theme
- Use a Child theme with a new functions.php
- Use a functions plugin
Using the Theme functions.php file
Although not mandatory most themes have their own functions.php file for theme specific functions.
You can add you own functions to this file, or modify existing functions using this file.
You can only modify existing WordPress functions if they are pluggable.
The pluggable functions are located in the pluggable.php file (in the includes directory), and not the functions.php
See creating pluggable functions below.
Using A Child Theme functions.php File
Using a child theme with its own functions.php file is the recommended way of adding custom functions, as they aren’t overwritten by any upgrades.
The functions.php file of a child theme is loaded before the parent theme functions.php file and the two files are additive.
That means that you get the functions from the child theme and parent theme.
Using a Plugin
If you need the functions to be theme independent then you can use a functions plugin to add the functions.
Generally this is the preferred method to override WordPress core functions.
Creating Pluggable functions -The Basics
To overwrite an existing function you cannot simple create a new function one with the same name as the existing one, as you will get an error.
However you can check for the existence of the function before you include it using the function function_exists()
so function_exists( ‘functionname’ ) will return true if the function has already been defined.
So assume we have function called f1 and we want to override it we simply use the following:
if(!function_exists( ‘f1’ )) // if it doesn’t already exist
function f1( ) //create it
//function code goes here
The function f1 is now pluggable
This article –understanding pluggable functions-goes into pluggable functions in much more detail.
It is possible to add your own functions to WordPress in several ways. Using plugins is probably the most versatile method.
When creating your own functions in themes it is considered good practise to make them pluggable, so they be overridden in child themes
Resources and References:
- WordPress Coding -Home
- Functions File Explained
- Codex Function Reference
- Use of Functions.php file
- Pluggable functions and their use
- Creating New Custom WordPress Page Templates
- Beginners Guide to the WordPress loop
- Understanding and Using WordPress Shortcodes
- PHP Callbacks and Callback Functions