My code snippets are presented as small, single file plugins that are easy to use - you just upload and activate.
When a post contains code snippets people often ask where to put the code. Many times the reader is directed to copy the code into their theme’s functions.php file (less the opening <?php line). I do things differently.
Why I dislike editing functions.php
I dislike the use of functions.php for a number of reasons.
- If the site owner makes any mistake copying the code into functions.php it is likely to break their site. If they are editing the file via the Appearance/Editor page in the WordPress admin area (a bad idea even at the best of times) they may not be able to get back into that page to fix the error. (WordPress 4.9 introduced PHP error checking so this is a little less likely now.)
- If the site owner wishes to disable the functionality it requires editing functions.php file again, with similar risks.
- If the site owner changes their theme then the functionality is lost until they copy the changes to the new theme’s functions.php file.
Easy to manage
My code is generally theme independent so it is still relevant when the theme is changed. Each snippet is presented as a single file plugin that can be activated and deactivated as necessary. In the unlikely event of a compilation error in the code, WordPress will not activate it and will report the error.
Step by Step
- Go to the post in question. In this demo I will be using the Customise “Choose an option” dropdown item post.
- Scroll down to the code snippet and click view raw to see only the code in question.
- Save this code-only page to your computer.
- Upload it to your site.
- Activate the plugin.
- Enjoy.
In this video of the process I save to a local environment to speed things up.
Note: If you use Microsoft Windows and copy the plugin source to Notepad, it will added the .txt extension. Remember to rename the file in Windows Explorer before trying to use the plugin.
Hi there.
I see you have the solution for changing/removing the “Sold out” on the product page. I am novice and I do not find out how to put the php text in the right place.
Do you have some instructions for that too?
Best regards,
Kai André
@Kai – You will use the Change Flatsome theme ‘Out of Stock’ label to ‘Sold’ code in the same as all my posts – download the code, upload it and activate it.
Hello thanks for your work,
I would love the sold text as a small buttom in the bottom left, what would the text be? I’m not a coder sorry..
Can you also help me how I can show this on single product page?
// Mark hire items as Unavailable instead of Sold. if ( is_hire_category($product->get_id() ) ) { return ‘Unavailable’; }
Kind regards,
Joey