There are many times that you want to add custom PHP code ( custom functionality ) to WordPress/WooCommerce website. Ideally, developers know all the pros and cons of each method. Today, I will show you the best practices. You can use any of the above methods to add a code snippet to your website.
Method 1 – With Third-Party Plugin
I will suggest a plugin Code Snippets. It will allow adding custom code quickly and safely. The custom code will work even after you switch the theme.
Method 2 – In Child Theme
There is no doubt that a child theme is the safest way of customization. Most of the themes provide a child theme, or you can create your own child theme within 10 minutes.
You just need to create two files, “functions.php” and “style.css”. Further, you can add PHP code snippets in “functions.php” file
Method 3 – Using WP Editor
WordPress contains a built-in editor that allows you to add a PHP or CSS code to your website. You can access the editor from the Dashboard -> Administration -> Appearance -> Editor. If you are not seeing Theme Editor, troubleshoot the same here.
Open the editor and go to the child theme’s functions.php. You can navigate to a child theme from the right-hand section. Add your code at the end of the “funtions.php” file. ( Before ?> this symbol, if you have it )
Method 4 – Using FTP
If you have access to cPanel of your website, you can use FTP client software like FileZilla to add custom code. Follow the given steps –
- Open your WordPress website via FTP.
- Go to wp-content -> themes -> your-child-theme.
- Right-click on functions.php and select View/Edit.
- Add your code at the end of the file and save it.