Managing code snippets in WooCommerce
/kb/managing-code-snippets-woocommerce/When working on a WordPress site, especially stores, you’ll likely reach a point where you need something custom. You might want to customize something that doesn’t have a true setting in WordPress? Or, you need to add a custom hook to modify something? Or, maybe you need to customize a part of your WooCommerce store?
No matter the case, making code changes means you’ll need to know the right place to do that. In this article, we cover the best ways to get this done and some best practices.
Working with code in WooCommerce
So first and foremost you should always be careful when working with the code on your site. A single mistype or stray character can have some seriously negative effects. Even if you’re not a developer you can still make some pretty impressive changes, you just need to know a few things first.
- PHP Code & Syntax
You should have a vague familiarity with PHP’s syntax, this can help ensure you’re using proper code. If you’re not familiar with PHP there are loads of useful resources for learning. (For now, you don’t need to set out to learn PHP, just knowing basic syntax is good.)
- Never blindly use code Even if you aren’t a developer or programmer by profession you can still make some changes. Reading over the resources linked above can go a really long way. You should personally read every line of code you intend to use. Even if you don’t get it yet, never use something without reading it over – if you do this and use the resources above it’ll start to click.
- Use trustworthy sources
Only use code from reputable sources (sites and people) is a great start. If you ever see something that doesn’t look right (super long, complex for a simple change, etc) then air on the side of caution and don’t use it. Generally, you can consider the code you find in the WordPress codex as safe. You can also find a lot of good stuff on GitHub too – but do your research first!
Another thing to be aware of is malicious code – especially if you’re using code you find online. Like PHP in general, you can find a lot of great code snippets for WooCommerce (and WordPress) online that cover common changes. If you don’t have experience coding the best thing to do is finding a plugin with the functionality you need. If you can’t then use code with caution – do your research and test things locally instead of live.
Add code to WooCommerce sites
So we know the risks of modifying WooCommerce with code and some basics of PHP syntax now. You did check out some basics at least, right? It’s good stuff to know and is easy to catch onto – especially if you’re going to be looking at it to tweak your site. If you didn’t brush up on the syntax we’ll just assume you know some basics already.
Using your active theme’s functions.php file
Now you have some code you want to implement in your site, but what’s the best way? Generally, the answer would be to pull up your theme’s files and edit the `function.php` file. Make sure you edit the active theme’s functions.php file, and if you’re using a child theme edit the child theme’s file. This file is one of the main areas that developers add custom functionality in WordPress. Depending on your situation this may not be an option, or maybe you aren’t sure the changes is permanent – in these case use the option below.
Using the Code Snippets plugin
On our Managed WordPress and Managed WooCommerce platforms, our team recommends using the Code Snippets plugin. This plugin provides an easy and direct way to add code snippets to your site.
Code Snippets provides an easy way to add different code snippets to your store, without having to manually add them to the functions.php file of your active theme. If you keep custom functions for your store in the active theme and change the theme at any point, those custom functions will not carry over into the new theme. As mentioned though you still will want a basic knowledge of PHP syntax.
When using this method, if the code snippets start with <?php, then copy all the code below that tag, without including the <?php.
You can add tags and then tell Code Snippets where to run the code. Your options are:
- “Run snippet everywhere” means that it runs on every page of your site without restrictions. On every page where plugins load, this snippet will be executed.
- “Only run in administration area” means the snippet code will only execute on `/wp-admin` pages — whenever is_admin() returns as true.
- “Only run on site front-end” means the snippet code will only execute on pages that are not part of the administration area — whenever is_admin() returns as false.
About the Author: Dan Pock
Dan Pock does technical support at Liquid Web with a background in System Administration, Public Relations, and Customer Service. His favorite things include his cats, Oscar Boots, and Dash Nougat; experimenting with PHP; and making up recipes (or at least attempting to). You can find his coding hijinks on GitHub, where he shares most of his projects and open source work.
Our Sales and Support teams are available 24 hours by phone or e-mail to assist.
What Is WebP and What Makes it Different from Other Image Formats?Read Article
Top 10 Password Security StandardsRead Article
Top 10 Password Security StandardsRead Article
How to Install MongoDB on AlmaLinuxRead Article
How to Use the WP Toolkit to Secure and Update WordPressRead Article