How to add custom code snippets in WPCode
If you want to add custom code to your site, you can use one of our action or filter hooks, add the code at the end of your theme’s functions.php file, or use one of the code snippets plugins.
Using a custom code plugin can be a better option for a few reasons:
- You get more control over your snippets
- You can activate them if you need to
- You can avoid possible errors
- You can use them on the backend, front-end, or only on specific pages
- Many, like WPCode, don't require you to have coding knowledge
In this doc, we’ll show you how to use WPCode - Insert Headers and Footers + Custom Code Snippets with MemberPress.
Video Tutorial
Once you install and activate the WPCode plugin in the plugins section, you’ll notice a Code Snippets section in WordPress Dashboard.
To add code snippets, first click the Add New button at the top of the Code Snippets section.
You'll then see a new Add Snippet window.
Next, hover over the “Add Your Custom Code (New Snippet)” section and click the Use Snippet button.
In the new window, you need to:
- Add a title
- Change the Code Type to PHP Snippet (so your code syntax is checked for errors)
- Add your code snippet to the “Code Preview” field
- Make sure the “Auto Insert” option is selected
- Check the “Inactive” checkbox field to make it “Active”
- Change the Location to “Frontend Only”
- Click the “Save Snippet” button to save the code snippet
That's it! Your code snippet should work.
Enabling and disabling code snippets is easy. All you need to do is to check/uncheck the boxes under the Status column in the Code Snippets table.
The location where your code snippet will run
In the example above, we used the mepr-account-nav-home-label filter hook to change the Home label on the Account page on the frontend.
Therefore, we used “Frontend Only” as the Location. This means the code snippet will run on the frontend only (that is, on all pages where regular users have access, including the Account page).
We could also use the “Run Everywhere” option. This way, the code would run on the entire site, including on the WordPress Dashboard (the backend).
If you'd like to use one of the filter hooks that run on the backend only (for example, mepr-admin-subscriptions-cols), you can use the “Admin Only” option from Location.
Conditional Logic
Please note that WPCode has other options where you can run code snippets. For example, you could run them only for logged-in users, specific pages, or for users who have a selected role.
To do so, check the “Enable Logic” checkbox in the “Smart Conditional Logic” section.
WPCode Snippets Pro Integration With MemberPress
If you WPCode Snippets Pro you can additionally control where and in what cases your code snippets will run based on the MemberPress location and conditions. Also, you can add your Conversion Pixels to track your MemberPress purchases.
Please check the following dedicated documents:
- WPCode Auto-Insert Locations for MemberPress
- WPCode Conditional Logic Rules For MemberPress
- Use WPCode Conversion Pixels With MemberPress
Help?
Is this not working how you think it should, even after following the instructions? Feel free to send us a Support Ticket!