How to add custom code snippets in WPCode
If you want to add custom code, for example, use one of our action or filter hooks, you can either add the code at the end of your theme’s functions.php file, or use one of the code snippets plugins. Using custom code plugin might be a better option because you have more control over your snippets, activate them if you need, avoid possible errors, or use them on the back-end, front-end, or only on particular pages. We’ll show you how to use one of the code snippets plugins, called WPCode - Insert Headers and Footers + Custom Code Snippets with MemberPress.
Once you install and activate WPCode plugin in Plugins section, you’ll notice Code Snippets section in WordPress Dashboard.
To add code snippets you need to click “Add New” button at the top of the Code Snippets section.
You will see a new Add Snippet window and all you need to do is hover over “Add Your Custom Code (New Snippet)” section and click “Use Snippet” button.
In the new window, you need to:
- Add title.
- Change Code Type to PHP Snippet so your code syntax will be checked for possible errors.
- Add your code snippet to “Code Preview” field.
- Make sure that “Auto Insert” option is selected.
- Check “Inactive” checkbox field so it will be “Active”.
- Change Location to “Frontend Only”.
- Hit “Save Snippet” button to save code snippet.
It’s done. Your code snippet should work.
Enabling and disabling code snippets is easy. All you need to do is to check/uncheck checkboxes under Status column in Code Snippets table.
The location where code snippet will run
In the example above I used mepr-account-nav-home-label filter hook to change Home label on Account page on front-end so I used “Frontend Only” as Location. It means that code snippet will run on front-end only so on all pages where regular users have access including Account page. I could also use “Run Everywhere” option, so my code would run on the entire site including WordPress Dashboard, so the back-end.
If you want to use one of the filter hooks that run on the back-end only, for example, mepr-admin-subscriptions-cols, you can use “Admin Only” option from Location.
Please note that WPCode has other options where you could run code snippets, for example run it only for logged-in users, specific pages, or for users who have selected role. To use it, you need to check “Enable Logic” checkbox in “Smart Conditional Logic” section.