Add code to WordPress – .com

Sometimes searching the Internet we find a “chunk” of code that we want to add to our WordPress. Let’s see how and where we have to place it.

It was precisely in yesterday’s tutorial “” that many of you asked me about the methods to “add” that code. This is very typical when someone searches the Internet for “How to do such a thing in WordPress”, and the solution they find involves placing a few lines of code “out there”. Well, let’s see those “there”. Let’s discuss three possible ways to place custom code, less recommended (worse) to most recommended (better).

Add custom code to WordPress (with a plugin)

This option is the easiest, especially for those who are terrified of having to edit a file with an editor, and when they are given a few lines of code they ask themselves “Now what do I do with this?”

In that case I recommend the use of a plugin that at the time I myself used a lot in my beginnings. Is named . The developer is Shea Bunge, a very nice guy, who knows what he’s doing, 100% recommended.

The plugin itself what it does is add a new option in the WordPress menu called “Snippets”, in which we can manage, add, import or configure the plugin.

A “snippet” comes to be (and may the translators forgive me) a “chunk” of code. In principle they are usually not very long, just a few lines, but they can be as long as we want.

So, we click on “Add New” and we find an interface that asks us for several things, the first being the title and the code:

In the image you can see a very simple example. The title is purely descriptive, just to clarify for us. And the code, in this case, is very basic, just one line. In the example it is a code that what it does is hide the web administration bar for all users.

In principle this is the only information we have to put. But there are other optional data that are worth filling in. The first is the range of that snippet.

By default it is applied “everywhere”, but we could make it apply only in the control panel, only on the web. This is a detail on the part of the plugin developer, so that the code is only loaded where it is necessary. Actually, this should be contemplated in the code itself with a conditional, but normally those snippets that are found on the net are not very optimized in that sense.

See also  SGAE course #3. Become a member - .com

Next we can put a description for ourselves, to be able to clarify ourselves and know what it does.

And finally we have a tagging system, in case we have a lot of snippets, we can easily organize them. Again, this is all optional:

Once we have everything done, we can “Save”, or “Save and activate”. The difference will be that the first option will only save that code, while the second option will also activate it. So, that means that we can have our collection of snippets ready, and only activate the ones that interest us.

To understand each other, it’s like having a collection of mini-plugins that we create ourselves by copying and pasting what we find on the net.

So, this is the first option we have. Like I said, very comfortable. The only problem is that actually this code is not in a .php file, where the code belongs, and where it should be, but it is stored in the WordPress database, so every time the page is visited it is it has to “look” for the code in the database, and apply it, an issue that affects the performance of the web, especially if there are many and they are very long.

Add custom code to WordPress (in functions.php)

Let’s go for the second worst option (or second best, depending on how you look at it)which consists of putting that code in functions.php.

This option is the most classic and typical, and surely if you look for solutions on the net, you will see that they usually always tell you something like “add this to the functions.php file” and such and such. Why? Why does this method seem to take the cake?

Well, basically because it is the simplest, since all the themes have a functions.php file, although it is not mandatory, the vast majority (seriously, practically 100%) has it. So, no need to install a plugin like Code Snippets (as we have seen before)nor to create a plugin of functionalities (as we will see below)simply copy and paste the code anywhere in the functions.php file and you’re done.

But watch out! Because it turns out that this file (functions.php) is within from the directory of our theme. This means that if at some point we change the theme, all those changes will go to hell, because they will be linked to the theme that we have active.

See also  Hosting comparison: DinaHosting |

And of course, if we want to keep them, we will have to copy and paste all those snippets one by one to the functions.php file of the new theme. Can you imagine how tedious that can be? Or even imagine that you are looking for a new theme and you want to try 5 or 6 themes, to see which one is best. You will have to copy all the code to all the themes, so that it doesn’t “pete” anything. In short, it is not worth doing it this way, because even if it is very fast, it is not optimal.

Add custom code to WordPress (in your plugin features)

And finally we come to the third and best option. Create your own plugin where to put all your custom code. In other words, all the snippets. That is undoubtedly the best.

What we will have to do is create a plugin (only the first time) where we will put all those snippets. Like I said, the first time it’s a bit more entertaining, but then it will be as simple as doing it in functions.php.

To create a plugin we simply have to create a folder inside the “plugins” folder with the name we want (for example my-plugin-functions)and there create a PHP file with the same name (following the example would be my-plugin-de-functions.php).

We will open that file and we will simply do the minimum necessary to create a plugin, which is this:

And that’s it. With that it would work, it is the minimum necessary for it to work. With that, the plugin will appear in the list of plugins and we can install it like any other.

And there we can put all those snippets that we need. For example, with the previous case, that of the administration bar, the plugin would be:

And that’s it. That would be enough to remove the admin bar. And from there, we could go adding as much code as we want, until we get fed up.

Actually this is a very simplified version of plugin. If you want to know more about how to create this functionality plugin, even without having to touch code, take a look at , specifically in the class, in which I explain more thoroughly through videos how to do it.

See also  Facebook Live Course #9. How to become a Facebook Partner - .com

And what do we gain by doing it this way, compared to putting the code in functions.php? Well, in this case our plugin does not depend on the theme that we have activeso that we can change the theme without any problem, since we have the one we have, our code (housed in a plugin inside the “plugins” folder) It will not stop working at any time, we have the theme that we have active.

Bonus: mu-plugin

And attention, tip for the most perfectionists. If you want to loop the loop, there is an even better practice than the previous one, or at least a “variation”. And it is that instead of placing the plugin that we created in the /plugins/my-plugin-de-functions/ folder, we can place it directly in a folder called /mu-plugins/.

That folder is a special folder that means that all the plugins that are there will be automatically activated without the need to activate them, and also there will be no possibility of deactivating them from the administration panel. In fact, “mu-plugins” means “Must Use” plugins. If we do it like this, our plugin will be indestructible, since it cannot be deactivated or deleted:

And now yes, we already have our bomb-proof armored snippets, without any problem in the event that we change the theme, and without the possibility of anyone deactivating or deleting them by mistake.

conclusion

There are many ways to add code to WordPress. We can do it through a snippet plugin, a theme’s function file, or by creating our own plugin.

We will have to choose the path that best suits us depending on our circumstances and ability to handle code and files in an FTP. But without a doubt, the best way is to create a “bombproof” plugin.

And what was said. If you want to know more, take a look at my , or even at , With the combination of both things you can do anything!

Loading Facebook Comments ...
Loading Disqus Comments ...