Why shouldn't I add my code to functions.php? Why did I touch on this topic?
It's as simple as that. Very often I see tips on the internet, like "add this code to the functions.php file of your topic and will %what you Googled%". This is a very frequent response, only the code that needs to be added changes. And this is the most common mistake of newcomers who somehow had to work with wordpress
This can only be done if these two conditions are met:
- If you are NOT going to switch to another topic at all, right at all.
- If there are NO updates on this topic, right at all.
After all, if the update is released, the whole thing will collapse and you'll have to recreate your custom pieces of code in functions.php again, which may not happen if you forget that you've piled up in a poor file of the wordpress thread.
The easiest thing to do is to use mu-plugins. It's just a folder with a set of scripts where you don't have to activate and invent anything. Created a file, scored a code, forgot.
There are also solutions through the creation of your plug-in in which you can place all this. I believe that this solution is not so practical, but can be suitable in some cases. For example, if this piece of code needs to be turned off. And it can be done without removing absolutely anything from the code, just click in the admin deactivation button.
Below, I'll show you how it's done.
How to make through mu-plugins
Open the wp-content folder at the root of the wordpress and look for the mu-plugins folder. Most likely, it just won't be. You can just create it, nn itself "grab" along with the content.
In this newly created mu-plugins folder, you can create a file with absolutely any name, let it be, for example, mycustomcode.php.
You can simply take and write your parts of the code into the file itself. Only the PHP tag open. I.e. something like this:
<?php //Your code
That's it, it's done. Everything is as simple as possible and now you can safely update anything, your code is not going anywhere.
How to make it through creating a plug-in
This method has its advantages. You can, for example, turn off your piece of code that you wanted to drive into functions through the admin. And then turn it back on. And turn it off again.
You can also edit your custom code directly through the admin, just like functions.php. Just click the "Change" button on the plug-in page.
This plug-in is created as:
Open the wp-content folder, then the plugins folder in which we create any folder. Preferably with a name that just won't be able to cross paths with any other plug-in. I'll create an evilcoder-custom-code folder. You can come up with your name.
In this folder, create a regular index.php, in which you need to prescribe a "cap" plugin, and then your code.
<?php /** * Plugin Name: evilcoder-custom-code */ //Your code
All. We save, activate in the admin - ready.
Something like this. You can leave your feedback and questions in the comments below, I answer them. )
Good luck, and never write different game in functions.php file working with wordpress