How easy it is to add your custom code in WordPress and not be afraid to break something

Often in our lessons we are writing that you need to add some code to the file functions.php or file a separate plugin to add new functionality to WordPress. This is the easiest and fastest way. But it has a drawback. If you make a mistake in your code, you can break the whole site.

In this tutorial we will show you how to protect yourself and easy to add your custom code in WordPress, not afraid to break something. And this will help us a great and free plugin Code Snippets.

The Plug-In Code Snippets

So, we need the plugin Code Snippets. It is free and it is possible to tell from the official WordPress plugin directory:

After activation you have in the sidebar of the admin will see a new menu Snippets.

Jump to this section here will be collected all your snippets are individual pieces of code to add various functionality:

Now this list will be empty, because you just installed this plugin. Let’s add our first snippet! Click on Add New.

A new window will appear, prompting you to specify a name for the snippet (the short description, what it will do) and the code below:

In the picture above we have added code that hides the version of WordPress installed on our website. Here is the code:

Under the block code there is a text box where you can add detailed description of what exactly this code does, where you found it and what purpose it has decided to apply.

You can also add tags to the snippet, to make it easier to sort and group.

When ready, click Save Changes and Activate. This will save and activate your snippet.

Or you can just click Save Changesif you don’t want to include it now, but just want to keep.

At the bottom you can select the scope of the attached snippet (Scope).

You have three options:

  • the snippet works everywhere on the website
  • the snippet only works in the admin
  • the snippet only works on the site without the admin panel

Error handling in the code that you added

As a rule, if you add code directly to a file functions.php and make there a mistake, your website may be screwed immediately.

At best, you won’t notice that somewhere inside the broken website functionality, in the worst case you will see a white screen with the error “500 internal server error“.

In this case, you will have to go to the site via FTP, look for and correct mistakes manually.

But with the plugin Code Snippets you protect yourself from these risks. If Code Snippets will detect your code mistakes, he will immediately report this and will not allow the snippet to turn on and break your website:

If the error is explicitly allowed in some line of code, the plugin will report the type of error and will indicate what line you need to make corrections. It’s cool!

Managing snippets

The Code Snippets plugin displays all of your saved snippets in the WordPress admin is exactly the same as normal posts or pages.

Which means you can the usual way to sort them, edit, enable / disable, delete and even export:

If you transfer the website to another server or hosting for WordPress, you can export all of your snippets and then import them on the new site menu Snippets → Import.

You can also view additional plugin settings in the menu Snippets → Settings:

Hope this tutorial helped you, and you will no longer fear to break something on the website in the process of adding code to functions.php. It is better to use Code Snippets!

Leave a Comment

Your email address will not be published. Required fields are marked *