Here at NameHero, we recommend using a custom plugin to paste your code snippets because it’s fully under your control and causes less bloat than plugins in the WordPress repository. However, it’s not within everyone’s comfort zone to create a custom plugin, particularly considering that if you configure it wrong, it can break your site. So if you use a plugin to manage your WordPress snippets, I suggest you use the WPCode plugin.
Centrally Managed WordPress Snippets
When you download and install WPCode, you’ll get an interface from which you can search for commonly used snippets, enable and disable them individually, convert them into shortcodes, and more. There’s also a section where you can add a header and footer code.
With these functions, WPCode provides a framework in which you can fit most of the WordPress customizations.
Creating a Custom Snippet
Let’s say you want to customize WordPress, and while searching the web, you see a PHP snippet that you need to copy and paste into your installation. With WPCode installed, select “Add Snippet” on the right-hand side of the dashboard to get started:
On the next screen, you’ll see many pre-defined code blocks. These are centrally managed and updated code snippets, so you can add all of this without needing to copy/paste any code yourself. If what you’re looking for isn’t here, then click “Add Your Custom Code” as shown in this screenshot:
On the next screen, choose the type of snippet you want to add. Chances are that you’re looking for a PHP snippet. So when you select that from the dropdown box, the editor will automatically add the PHP tags at the top of the code, so you don’t have to. Paste the code you want into the space below:
For this example, I’ve chosen to use a code snippet to add some footer text to the bottom of your website. It’s important to remember that if the code you’re copying already has these PHP tags:
Then you need to remove these tags from the code you paste because WPCode already places them there for you. But the great thing about this plugin is that even if you make a mistake, your site won’t crash. More on that below.
Choose the Snippet Location
WPCode allows you to choose whether you want the snippet to execute only on the admin screen, front-facing pages, or everywhere on the site. This means you can remove conditional statements from your snippets that check whether the existing screen is an admin screen or not.
In addition, you can also configure the snippet to run as a shortcode whenever you paste the shortcode into your posts or text widgets, as shown here:
This means you no longer need to create separate shortcode snippets – I always need to re-learn the syntax whenever I create a new shortcode, which is very useful!
Finally, you can enable are disable your snippet from the central list whenever you want:
Protection in Case of Errors
Normally if you make a syntax error in the code, or if someone gives you malformed code, it can utterly break your site. There are so many posts online describing how to recover from a site crash that an incorrect snippet caused. With WPCode, you don’t need to worry about this.
For example, I deliberately made a syntax error in my plugin, and this is what happens when I try and enable it:
As you can see, WPCode deactivates the snippet automatically and generates an error message that hints at what went wrong. You can click the “Edit” button below the snippet to edit the error. This functionality generates a great deal of peace.
Great for Beginners and Advanced WordPress Users
Advanced WordPress users will appreciate the import/export functionality and the “Generate” functions that quickly create code snippets for various tasks. And those new to WordPress don’t have to worry about code snippets breaking their site, and gives them a central location from which they can manage their snippets. One of the “must-have” plugins on all new WordPress installations!
I’m a NameHero team member, and an expert on WordPress and web hosting. I’ve been in this industry since 2008. I’ve also developed apps on Android and have written extensive tutorials on managing Linux servers. You can contact me on my website WP-Tweaks.com!
Leave a Reply