If you really want your website to look and behave the way you want, you have 2 main possibilities. Add a lot of plugins or add custom code. While the first solution is often easier to set up, it can become a performance nightmare. Therefore, in some cases, you will have to add some code to add a custom feature to your WordPress website.
Luckily, this guide will make it easier for you and explain everything you need to know to do so. Even if you’re not a developer. And, before we begin, here’s a general rule you should follow when deciding whether or not you should install a plugin :
If you plan on using less than 10% of a plugin’s features, don’t install it and look for other solutions
This one is from us, you're welcome
1|Installer Code Snippets
On the internet, if you run searches on how to add custom features to your WordPress website, a lot of answers will tell you to add the code to your theme’s functions.php file. Don’t do that !
Not only will it become messy over time as you add more code, but it will also run this code everywhere. Even if it’s not needed. Instead, you can use a free plugin called Code Snippets that presents many benefits :
- It lets you organize your code in small snippets
- You can enable or disable your code snippets at any time
- It lets you decide if the code runs on the backend, frontend or both
- You can export/import your code snippets from a staging website to the live one
- If the code is wrong, this plugin will prevent it to run and crash your website.
For all these reasons, we advise you to use this lightweight and free plugin.
2|Comprendre les hooks WordPress
You don’t need to be a developer to understand this guide. In addition, you don’t need to know PHP in order to use it. But if you want to add a custom feature to your WordPress website, you need to understand some concepts. If you learn these, you’ll be much more capable of finding the code you need.
WordPress relies heavily on hooks. Some might even say that hooks is the main WordPress feature. In fact, hooks are what allow WordPress to be highly and easily customizable.
Definition : Hooks are a way for one piece of code to interact/modify another piece of code at specific, pre-defined spots. They make up the foundation for how plugins and themes interact with WordPress Core, but they’re also used extensively by Core itself.
To simplify it for you, let’s put it that way. All WordPress and plugin developers add doors in different places where you can add your own code and change a specific behavior.
1|Hook Creation
Developers can add 2 types of hooks into their code : Actions and Filters.
- an action takes the info it receives, does something with it, and returns nothing. In other words: it acts on something and then exits, returning nothing back to the calling hook.
- a filter takes the info it receives, modifies it somehow, and returns it. In other words: it filters something and passes it back to the hook for further use.
Here’s an example of a filter
$value = apply_filters('the_name_of_my_filter', $data_sent);
With the above line, the developer created a hook called 'the_name_of_my_filter' which will send the information $data_sent. The result will be stored into $value.
2|Se brancher sur un crochet
If you want to add more information or add a custom feature, you will probably have to use an existing hook and add your code to it.
If we look at the above filter, here’s how to plug into it
add_filter('the_name_of_my_filter', 'theNameOfYourFunction');
Here you said you wanted to call the code from the theNameOfyourFunction function inside that existing hook. However, that's half the battle. You still need to add your custom function.
function theNameOfYourFunction($data)
{
/** Add your custom code or feature here */
return $data;
}
There you have it, your code will be called every time the hook is called. Is this still a bit obscure? No problem, everything will become clear with the following example
3|Ajoutez une fonctionnalité personnalisée à votre site Web WordPress
Now that you know how hooks work, it’s time to put that knowledge to use with some real-world examples. First, I’ll give you an example. Then, I’ll give you some tips on how to find custom code that fits your needs on websites like this one or Stack Overflow .
Give users a specific role when they sign up
In this code, we will give the “VIP” role to users when they create an account on your website. If the user role does not exist, we create it.
/** When a user creates an account, WordPress uses the 'user_register' action hook */
\add_action('user_register', 'addVipRole');
/** At this point, the user is already created, we only need to change his role */
function addVipRole($userId)
{
/** First, we make sure that the user exists */
if( $user = \get_user_by('ID', $userId)) {
/** We set the role we want to give to the user */
$role = 'vip';
/** We test if the role exists. Otherwise, we create it */
if (!\wp_roles()->is_role($role)) {
$role = \add_role('vip', __('VIP User', 'testdomain'));
}
/** Now we give the role to the user */
$user->set_role($role);
}
}
It’s that simple. If you want to display other information or run a shortcode, that’s possible too. All it takes is a few searches.
4|Ressources utiles
Business Bloomer Visual Hook Guide : Here you will find many hooks that you can use to display information or add your own content on many different pages. This is a very useful resource, especially if you are using WooCommerce.
Stack Overflow : This website is commonly used by many developers and it has a large community. They are also willing to help people who are not developers and give you step by step guides. You will find a lot of useful code snippets there.
Our Plugins
We have created powerful and widely acclaimed plugins for WooCommerce. Boost your sales with our solutions
WooRewards
Discover the most powerful loyalty plugin for WooCommerce. Simple or tiered systems, referrals, social networks, badges and achievements, you will find all the tools to build YOUR loyalty program
Learn MoreVIP Memberships
VIP Memberships is a complete membership management tool for your WooCommerce site. Sell subscriptions to your customers and offer them benefits such as preferential prices or exclusive products.
Learn MoreVirtual Wallet
Offer your customers a virtual wallet on your website. Let them save money by purchasing your products and use this credit on future purchases. This extension also offers a complete gift card tool
Learn MoreReferral Codes
Win new customers with this complete SEO tool. Whether through influencers or simple referrers, reward them and the new customers they bring
Learn More