How to Add Custom Code to Your WordPress Site (It’s Easier Than You Think!)

By WP Saviour •  Updated: 03/07/21 •  8 min read

Writing PHP, CSS, and HTML5 to switch a WordPress web site can get fairly irritating, however a very good information could make it simpler. This isn’t that information. That is nearly the right way to add the code to your web site as soon as it’s already completed. Don’t fear, although – that’s simply as vital. As there are fairly a couple of methods to go about it, it helps to know the variations between the varied choices. However first, two warnings:

  1. Earlier than you modify something, again it up. In reality, preserve backing up your work all through the method. It additionally helps to have a separate file to stick discarded or soon-to-be-used code snippets into.
  2. In the event you put in another person’s theme, by no means add code to your theme’s features.php or model.css recordsdata. In the event you do, then when the theme will get up to date, all of your arduous work might be worn out.

Over the course of the remainder of this text, I’ll discover some alternate options, organized from the only to probably the most complicated.

WordPress Plugins


Simply as there are a number of kinds of code modification, there are a number of kinds of {custom} code plugins. The two most typical sorts are PHP and CSS. As it’s possible you’ll know, PHP governs the structure and performance of WordPress. So, in case you’re making an attempt to switch your web site’s core features, add, or modify a characteristic, there are many PHP plugins accessible for obtain to help you with that.

Likewise, you in all probability know what CSS does, too – it pretties issues up. And if that’s all you need to modify, a Customized CSS Plugin will allow you to do this with minimal trouble. Lurking within the background is JavaScript, which has a fancy relationship with plugins. To make clear, it’s a bit totally different in that it really works completely effective in case you add it to the header, the footer, or a baby theme – however with the intention to use it in a submit, it’s important to use a particular plugin. One thing like CSS & Javascript Toolbox ought to do the trick. As a facet observe, for the sake of your web page’s pace, it’s higher to place it within the footer.

Writing Your Personal Plugins

There are a ton of WordPress code snippets accessible on the web to vary, disable or modify sure characteristic. Nonetheless, figuring out the place to place them is essential for performance. For instance, if in case you have a snippet that modifies a plugin’s operate, you’ll need it to work it doesn’t matter what theme you have got activated. The identical goes for snippets that cope with site-wide alterations. So, in both case, it may be a good suggestion to create a brand new plugin for them. Don’t fear, it’s not as intimidating because it appears.

One of many major advantages of making a plugin is that it’ll mean you can activate or deactivate the plugin on demand. It additionally permits contextual details about the plugin to be considered in plugin pages. The one main disadvantage is that it may be by chance deactivated by anybody else who’s allowed to switch the location, however a stern warning to them ought to be capable to stop that.

As a facet observe, it’s also possible to create a folder known as mu-plugins in your wp-content folder. Any PHP recordsdata inserted there’ll all the time be executed, they usually can’t be activated or deactivated. This retains {custom} code away from theme and plugin folders. However solely do it with plugins that you simply’re all the time snug working within the background.

Customized CSS Recordsdata

If the modifications you’re planning are minor, you could possibly create a custom-style.css file. If you wish to preview your modifications earlier than making them, you possibly can all the time use DevTools in case you’re working Chrome, or an analogous utility. One other chance is so as to add {custom} CSS by way of your WordPress theme. Some premium themes, like our Complete framework, embody a theme panel choice so as to add {custom} CSS the place you possibly can simply discover and tweak it. Plus whenever you replace your theme your theme panel settings ought to stay unchanged – so your {custom} CSS stays intact.

Do not forget that CSS cascades in reverse, which means guidelines that seem later all the time take priority over guidelines that seem earlier. So any CSS utilized to a separate sheet will override the web page’s default code. That additionally applies to CSS inserted into…

WordPress Little one Themes


For the uninitiated, a baby theme inherits the performance of one other theme, permitting you to switch your design at will (you possibly can be taught every little thing you need to know in our WordPress little one themes information). They had been invented for the particular function of letting you modify a theme with out shedding your modifications upon updating. Little one themes mirror all of the modifications that you simply make to the outer, or ‘mother or father’, theme. So, like making use of a Photoshop layer impact, you’re not really enhancing the content material; you possibly can take away it at any time to get issues again to the best way they had been.

Creating a baby theme whenever you need to modify your code will prevent plenty of complications, and it’s pretty straightforward (as WordPress modifications go). Simply entry your web site by way of FTP and create a folder, then create a model.css file that corresponds to that folder. It’s commonplace apply to call it theme-name-child. After that, all you want is one line in your new model.css’s header to outline the template. However there’s additionally some stuff that it helps to have – which could be discovered on the WordPress Codex web page.

Subsequent, make any essential modifications to the present theme beneath the @import line. The one disadvantage to little one themes is the quantity of analysis it’s important to do on the mother or father theme earlier than you possibly can modify it, with out inflicting any conflicts or bizarre unwanted effects. And do not forget that with little one themes, you solely must specify what might be totally different from the unique theme. To use the modifications, simply ZIP it, add it by way of the Appearances > Themes web page in your WordPress dashboard and activate it by way of WordPress. That is assuming your mother or father theme can be uploaded, after all.

Displaying Code


There’s yet one more kind of including code that we haven’t coated: displaying it. In the event you’re an online designer and want to present how one thing’s completed, introduced it in a means that viewers can copy and paste, it’s vital to notate it the right means. The way in which your code might be interpreted by WordPress is dependent upon whether or not you utilize the HTML part of Visible Submit Editor. Coming into it immediately into the visible editor will work to neutralize it, however there’s no means of placing it within the correct formatting or a code field. The visible editor will simply format it the identical means as abnormal textual content. Then again, the HTML editor will flip it into code and spoil your web site. Don’t fear, you possibly can undo that.

Nonetheless, there’s a compromise. You possibly can spotlight and put your code in a block utilizing the and tags, however that received’t cease the code from activating. All it’ll do is put it in montoype font, in a field, to indicate that it’s formally code. As a way to cease it from activating, it’s important to change the lively symbols to what are known as enjoyable character entities. The commonest variations of those, and those which might be ample for neutralizing most code, are altering < and > to < and >, respectively (you possibly can be taught extra about displaying code in our submit on writing {custom} code in your WordPress posts).

gp-2150918 as-6026615

WP Saviour

I am a WordPress specialist. My mission is to help you create beautiful websites with ease!