How to add your custom code to WordPress and not break something

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

In this lesson, we show you how to secure yourself and easily add your custom code to WordPress without fear of breaking something. And in this, we will be helped by a wonderful and free plugin Code Snippets.

Plugin Code Snippets

So, we need the plugin Code Snippets. It is free and it can be said from the official WordPress plugin catalog:

After activation, a new section of the menu Snippets appears in the side menu of the admin area.Go to this section, here will be collected all your snippets – separate pieces of code to add different functionality:

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

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

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

function wpb_remove_version () {
return '';
}
add_filter ('the_generator', 'wpb_remove_version'); 

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

You can also add labels to your snippet so that it’s more convenient to sort and group them later.

When everything is ready, click the Save Changes and Activate button. This will save and activate your snippet.

Or you can just press Save Changes if you do not want to turn it on now, but just want to save it.

At the very bottom, you can select the scope of the added snippet ( Scope ).

You have three options:

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

Error management in the added code

As a general rule, if you add the code directly to the file functions.php and make an error there, your site may get immediately.

At best, you will not notice that the functionality is broken somewhere inside the site, at worst you will see a white screen with an error “ 500 internal server error “.

In this case, you will have to go to the site via FTP, search and fix the errors manually.

But with the Code Snippets plug-in you will protect yourself from these risks. If Code Snippets detects an error in your code, it will immediately report it and will not allow this snippet to turn on and break your site:

If the error is explicitly allowed in some line of code, the plugin will report the type of error and indicate which line to correct. It’s cool!

Management of snippets for Custom Code

Code Snippets plugin displays all your saved snippets in the WordPress admin panel just like regular entries or pages.

So, you can customarily sort them, edit, enable / disable, delete and even export:

If you transfer the site to another server or hosting, you can pre-export all your snippets and then import them on the new site in the Snippets → Import menu.

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

I hope this lesson helped you, and you will not be afraid anymore to break anything on the site while adding code to functions.php. Better use Code Snippets!

Source: wpbeginner.com

 

Leave a Reply

Ad Blocker Detected

Our website is made possible by displaying online advertisements to our visitors. Please consider supporting us by disabling your ad blocker.

Refresh