Skip to Main Content

How to easily add code to WordPress without breaking your site

Divi: the easiest WordPress theme to use

Divi: The best WordPress theme of all time!

With over 701.000 downloads, Divi is the most popular WordPress theme in the world. It is complete, easy to use and comes with more than 62 free templates. [Recommended]

Often times when reading WordPress tutorials you may be prompted to add custom snippets to your theme's functions.php file or a plugin. The problem is, even the smallest error can render your site unusable. In this tutorial, we'll walk you through a simple way to add custom code to WordPress without breaking your site.

How to add custom code on wordpress

The problem with custom code snippets

Often, you'll find code snippets in WordPress tutorials with instructions to add them to your theme's functions.php file or to a specific plugin on your site.

The problem is that even a small error in the custom snippet can make your site inaccessible.

The other problem is that if you add multiple snippets in a specific plugin, it can become difficult to manage the file.

Recently while researching a solution, we discovered an easy solution for allowing users to add and manage custom snippets on WordPress.

Add custom code snippets to WordPress

The first thing to do is to install and activate the plugin « Code Snippets " on your website. For more details, see our step by step guide on how to install a WordPress plugin.

Upon activation, the plugin will add a new menu item titled Snippets on your WordPress admin bar. Clicking on it will bring up a list of all the custom snippets you've saved to your site.

As you have just installed the plugin, your list will be empty.

Go ahead and click on the button Add New To add your first custom code snippet to WordPress.

Add a custom code snippet on wordpress

Easily create your website with Elementor

Elementor allows you to easily create any website design with a professional look. Stop paying expensive for what you can do yourself. [Free]

This will take you to the page " Add a new extract ».

You must first enter a title for the code snippet. This could be anything that helps you identify the code. Then you can copy and paste your code into the code box.

Add a code in the code box

In the screenshot above, we've added a custom code snippet to remove the WordPress version number on our test site.

Function wpb_remove_version () {return ''; } Add_filter ('the_generator', 'wpb_remove_version');

Below the code box you will see a text box to add a description. You can add something here that helps you understand what this code does, where you found it, and why you are adding it to your site.

Add an interface description code

You can also assign tags to your code snippet. This will help you sort your code snippets by subject and feature.

Finally, you can click on the button Save changes and activate ».

Are you looking for the best WordPress themes and plugins?

Download the best plugins and WordPress themes on Envato and easily create your website. Already more than 49.720.000 downloads. [EXCLUSIVE]

If you just want to save the snippet and not activate it, you can click the " Save Changes ».

You can also change the scope of the snippet. You can run it only on the dashboard, on the front-end or anywhere. If you are not sure, keep the default option " Run the snippet everywhere ».

Once you save and activate the snippet, this code will automatically apply to your site.

Manipulation of errors in custom code

Normally, if you make a mistake adding custom code in your site-specific plugin or file, this will make your site inaccessible.

You will start to see a syntax error or 500 error on your site. To resolve this issue, you will need to manually cancel your code using an FTP client.

The neat part of the Code Snippets plugin is to automatically detect a syntax error in the code and disable it immediately.

Syntax error detectedIt will display a message, so that you can debug the error.

Manage your custom code snippets

The plugin « Code Snippets Provides a simpler graphical user interface for managing your custom code snippets on WordPress.

You can save snippets without activating them on your site. You can deactivate the extract at any time. You can also export specific snippets or export them.

Management of code snippets on wordpress

If you are transferring websites to a different server, you can easily import your snippets using the plugin " Code Snippets »By visiting the page« Import Snippets ».

Easily create your Online Store

Download free WooCommerce, the best e-commerce plugins to sell your physical and digital products on WordPress. [Recommended]

Import snippets

The plugin « Code Snippets Works without configuration, but you can change the plugin settings by visiting the Extract Setup page.

Wordpress extract settings

That's all for this tutorial, I hope it will allow you to better manage code snippets on your blog.

This article features 0 comments

Leave a comment

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

This site uses Akismet to reduce unwanted. Learn more about how your comments data is used.

Back To Top