In this post we’re taking a look at how to add custom functions to a WordPress site.
First let’s consider why you would need to do this. In many instances when building a WordPress site you may need additional functionality to be added to the site there are a few different ways to do this, these include:
How to add Functions:
Adding PHP directly to your sites files:
Adding PHP directly to your functions.php file – The functions .php file is often one of the quickest and easiest ways to add a new snippet of code. You can find this within your theme files on your site.
However before adding any code to your functions.php file it’s important that your site either has a child theme or a custom built theme. If you are using a purchased theme or a free theme that receives updates from the author if you add any code directly to your functions.php file it will likely be overwritten during the next update. To get around this it’s important to have a child theme in place with a functions.php file held directly in this. That way when the parent theme is updated the function that you have added will remain active.
To access your theme files it’s always advised that you ensure that you have a full backup copy of your website and access to the hosting side of things. Following this you can access the code via FTP by connecting via your host and opening wp_content > themes > your theme. Other methods for accessing your files can include using the file manager within your hosting account, the usage of this will depend on the type of hosting you have and the interface that it features.
Once you have access to the functions.php file be sure to paste or type your code below any other code snippets and ideally add comments for reference points throughout. If you’re unsure of how to add code comments within PHP take a look at our article here.
Using a Plugin:
Whilst we would generally suggest adding functions directly using the method above, that may not always be possible or necessarily practical. As such there are a wide range of plugins available that allow you to add code directly to your WordPress site. Many of these are free and either come as part of an overall code additions plugin or a PHP specific plugin. We’ve featured some good ones below but there are tonnes available!
Direct Link: https://wordpress.org/plugins/my-custom-functions/
One of the key benefits to using a plugin is that many come with toggles that allow you to enable and disable the PHP code snippet depending on when you need it functionally within your website. Another great feature of many of these plugins is the ability to add PHP and then use it as a shortcode throughout your site for specific functionality.
Another useful aspect is the organisation of your code snippets, by using a plugin you can structure your PHP snippets based on the titles and their functionality within the site making it easier to find and edit specific functions. Whilst this is also perfectly achievable with adding the code directly and using well organised code comments.
An important thing to consider about plugins however is that the more you have the more bulk it adds to your website. The plugin has its own files and assets as well as running the PHP code, whereas by directly adding the code to your website you’re literally only adding that function.
WooCommerce adding text before your add to cart with PHP
This has been a quick insight into two great ways to add PHP functions to your website using the direct code method or by utilising a plugin. We hope that this has helped and if so we’d really appreciate any comments or a share thanks!