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 !
Non seulement cela deviendra compliqué au fil du temps à mesure que vous ajouterez du code, mais cela exécutera également ce code partout. Même si ce n’est pas nécessaire. À la place, vous pouvez utiliser un plugin gratuit appelé Code Snippets qui présente de nombreux avantages :
- Il vous permet d’organiser votre code en petits extraits
- You can enable or disable your code snippets at any time
- Il vous permet de décider si le code s’exécute sur le backend, le frontend ou les deux
- Vous pouvez exporter/importer vos extraits de code d’un site Web de test vers le site en direct
- Si le code est erroné, ce plugin l’empêchera de s’exécuter et fera planter votre site Web
Pour toutes ces raisons, nous vous conseillons d’utiliser ce plugin léger et gratuit.
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.
Voici l’exemple d’un filtre
$value = apply_filters('the_name_of_my_filter', $data_sent);
Avec la ligne ci-dessus, le développeur a créé un crochet appelé ‘the_name_of_my_filter‘ qui enverra l’information $data_sent. The result will be stored into $value.
2|Se brancher sur un crochet
Si vous souhaitez ajouter plus d’informations ou ajouter une fonctionnalité personnalisée, vous devrez probablement utiliser un crochet existant et y ajouter votre code.
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;
}
Voilà, votre code sera appelé chaque fois que le crochet est appelé. C’est encore un peu obscur? Pas de problème, tout deviendra clair avec l’exemple suivant
3|Ajoutez une fonctionnalité personnalisée à votre site Web WordPress
Maintenant que vous savez comment les crochets fonctionnent, il est temps d’utiliser ces connaissances avec quelques exemples réels. Tout d’abord, je vais vous donner un exemple. Ensuite, je vais vous donner quelques conseils sur la façon de trouver du code personnalisé correspondant à vos besoins sur des sites Web comme celui-ci ou le site Stack Overflow .
Donnez aux utilisateurs un rôle spécifique lorsqu’ils s’inscrivent
Dans ce code, nous donnerons le rôle “VIP” aux utilisateurs lorsqu’ils créent un compte sur votre site Web. Si le rôle d’utilisateur n’existe pas, nous le créons.
/** 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);
}
}
C’est aussi simple que ça. Si vous souhaitez afficher d’autres informations ou exécuter un shortcode, c’est également possible. Tout ce qu’il faut, c’est quelques recherches.
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