Adding custom code into Must Use Plugin

Developer Knowledge Level

This content is intended for WordPress developers, and it may require coding knowledge of WordPress, PHP, and JavaScript. Code examples provided here may contain errors or needs some additional coding. Make sure to test the code before using it on a live website!

In most cases, custom code to expand various plugins is added to functions.php in the active theme. And, that is just an awful way to add custom code for many reasons: that code is not related to the theme, so why add it to the theme. And, if you change the theme, you need to copy the code over.

The best way to add the custom code is by using Must Use Plugins. WordPress supports Must Use Plugins or mu-plugins since version 3.0, and they are the best way to add custom code to WordPress for the purpose of tweaking. Must-Use plugins don’t need to be activated, WordPress will load them always.

Must Use Plugins Folder

Make sure that your website has a folder called ‘mu-plugins‘ in the ‘wp-content‘ folder, and if there is no such folder, make it.

Must Use Plugin File

Once you have that folder, create a new PHP file in that folder. For example, make the empty file called ‘my-custom-code.php’. When empty, this file needs only to have php opening tag:

<?php

// add custom code here //

You can add multiple files, and even name them according to the plugin you are adding custom code to better organize the custom code you use.

When you need to add some tweak, function, or any other PHP custom code, add it in this file, just like you would add it to the theme functions.php file.

Special Considerations

Anytime you make changes to the must-use plugin(s), I highly recommend testing changes on the development or staging website first!

0
0
275
Rate this article

You are not allowed to rate this post.

Leave a Comment