Many of the answers on this website include the custom code you need to use (and maybe modify) to achieve something related to bbPress. What is the best way to add this code?
Most of the time, when there is a custom code involved with WordPress, recommendation is to add it to the active theme functions.php. I think that is a very bad idea. What if you change the theme? You need to hunt all the custom code from previous theme and copy it into new theme. Maybe you copy something specific to the old theme, and you break your website.
The best way to add the custom code is via the plugin. But, if you don’t want to create the plugin for few lines of code, the best solution is to use Must Use Plugins, or ‘mu-plugins’. These plugins are always loaded, they are usually a single file, and they don’t support many advanced features normal plugins have. But, for our purpose, they are perfect.
Create mu-plugins directory
Before you create a mu-plugin, make sure that you have mu-plugins directory under wp-content. If you don’t have this directory, create it (using FTP or the file manage from your hosting control panel). Image on the right shows how the wp-content directory looks like when the mu-plugins is present. On your website, you might have some more directories. But, it is important to have mu-plugins.
Create new mu-plugin
Now, in the mu-plugins folder, create a new plugin. Let’s call it bbhelp.php. This will clearly state that plugin will hold the code from bbHelp Club website, and you will be able to find it and modify any time you need.
Once you create the file, open it for editing, and insert the following few lines to get started:
<?php /** * Custom Code for bbPress from bbHelp Club website * https://bbhelp.club/ */
Now, at the end of this file, add empty line, and paste the custom code from bbHelp answers you want to use. Save the file, and you are done. WordPress will load this file anytime it loads, and will run the custom code inside. Here is the file with some custom code added from the How can I disable adding of ‘nofollow’ to content links? answer:
<?php /** * Custom Code for bbPress from bbHelp Club website * https://bbhelp.club/ */ // disable adding 'nofollow' for topics remove_filter('bbp_get_topic_content', 'bbp_rel_nofollow', 50); // disable adding 'nofollow' for replies remove_filter('bbp_get_reply_content', 'bbp_rel_nofollow', 50);
If you find another answer with custom code you want to use, add empty line at the end of bbhelp.php file in the mu-plugins directory, paste the custom code from the answer, and save. That’s it.
As you can see, mu-plugin is very easy to use, and very convenient method for adding bits and pieces of the custom code, without messing with the theme or other plugins. And, you can use it for other things, and you can add more mu-plugins, just make each plugin a new PHP file.
Let me know if you have you used mu-plugins before, and what do you think about them?