Adding custom PHP code to functions.php

 Every webshop is different and so are your desires for how your webshop should function. Maybe you have already chosen a WooCommerce theme that has almost all the functions you need, but there are still regular adjustments needed to improve your website. Many of these adjustments can be made directly in the functions.php file of your child theme. In this article, you will learn everything about this file and how to deal with it.

Edit the function.php file

What is the functions.php file and where can I find it?

WordPress has chosen a specific file structure for themes, and the functions.php file is part of this. As the name suggests, this file should contain functions. When a theme is loaded by WordPress, the system also looks at which functions are called in the functions.php file. This happens quite late in the process, so you can overwrite or even disable existing functions.

So, every theme has a functions.php file. If the file is missing, the theme won’t work and you will get an error message. You can find the file through an sFTP connection to your website in the /wp-content/themes/yourtheme/ folder.

Please note that you should only modify the file in the theme folder if it is a theme specifically written for your website. In almost all cases, you should use a child theme…

No WordPress website without a child theme

If you use a theme chosen from the WordPress repository or have purchased a premium theme from a WordPress theme developer, you will receive regular updates. During such an update, all the changes you have made to your theme will be overwritten.

To prevent this, always use a child theme. If you don’t have a child theme yet, it’s time to create one!

How can I edit the functions.php file?

When you install WordPress by default, you can also edit your functions.php file via the dashboard. We advise you not to do this. This way, you won’t have a backup of the original version, and if you enter bad code, it’s possible that even after a check by WordPress that your website will stop working. If you don’t have sFTP access, you may not be able to fix the error quickly. You can better do it the following way:

  1. Download the functions.php file to your laptop and save a copy as a backup.
  2. Open the file in a PHP editor. We recommend Brackets, a free text editor that works with Windows and Mac.
  3. Now edit the code and save the file.
  4. Are you sure everything is correct? Upload and replace the current file.
  5. Test your website to see if everything still works.

Are there any errors or unexpected behavior after your update? Then it might be better to ask a web developer. Do you want to make major changes? Then test your new code first through a staging or test location.

Frequently Asked Questions

Where can I get a child theme for my theme?

Most premium WordPress themes also offer a child theme. If the child theme is missing, you can create it yourself. As mentioned earlier, a child theme consists of at least two files. You can find a child theme for OceanWP here. With some minor adjustments, it can also work for your WordPress theme.

What can I do if my website stops working after changing the functions.php file?

If you have saved the old file as a backup before making changes, replace the modified file with the previous version. Did you not make a backup before the change? Then use the file from a backup made by your hosting provider. Do not replace everything, only the functions.php file. After all, you don’t want to revert your entire webshop.

Is it safe to modify the functions.php file?

Modifying a PHP file is as safe as your knowledge of PHP code and web development. Not sure or do things go wrong when making changes? Ask a WordPress specialist.

Where can I find code snippets that are safe for my webshop?

There are some code snippets on the WooCommerce website that are generally safe. Your WordPress theme developer’s website may also have useful code snippets. You can usually trust these since the same person developed your theme. Don’t use PHP code you find on other websites without checking it first.

How can I modify other files from my (parent) theme?

A WordPress child theme consists of at least the files “functions.php” and “style.css”. In principle, you can also copy the other theme files and modify them. Our advice is to only override theme files if there is no other option. Your theme developer may also modify these files in future versions, and you may miss important improvements (or your website may not work properly after a theme update).

Published in: PHP Scripts · WordPress Development

Leave a Reply

Your email address will not be published. Required fields are marked *

Don’t post your code here!
Post your code block or snippet to pastebin and include the pastebin URL in your comment.

I delete all comments with non related links inside the comment text. Keep your comment related to the topic, if your question is off-topic, please use the contact form instead.