Snippet WordPress, what are they and how to add them? –

It is very common that when we are developing a WordPress website we always look for ways to extend features and add features that we do not have by default, many of these features are usually added through plugins that are specifically developed to allow us to incorporate the functionality that we want. , but in some cases it is much more practical to add these features that we need through small fragments, in our case, WordPress snippets. Instead of or plugin we add the features we want.

Hosting Mautic QuickStart -50% with support in Spanish
24 hours and free training

Send up to 1,000,000 emails/year. Mautic support in Spanish 24 hours, 365 days

In this post we will see what WordPress snippets are and how we can safely add them to our website.

What are snippets?

Snippets in programming refer to code fragments that can be reused anywhere we need, be it on our website or a system created from scratch, these usually solve specific problems or incorporate additional features that we do not have by default, in this way we can work faster and add these features to our website without having to write new code or alter the current WordPress code or theme that we use.

We usually add these code snippets within WordPress to the theme file, an important recommendation is to use a child theme within WordPress and add these codes within it, this provides us with greater , since at the time of not losing this feature that we are incorporating into our website.

How can we add the code snippets in WordPress safely?

Adding these snippets in WordPress is very simple, but it is very important to make sure to add this code correctly, since adding them incorrectly can break our site or remove important information, keeping this in mind we will see different methods with which we can add code plugin in WordPress that allows us to incorporate these features.

Many of them allow many default features and customization options; However, sometimes we need to extend the functionality of the theme even more, one of this example we can see when we have one and we want to show some additional options in the products view, let’s see the live example.

Suppose we have a store where we want to display the short description that we add in the products on our store page.

See also  Pages not secure and images are missing

In this case, some themes may have the built-in functionality by default, but what happens if we use a theme that does not allow it, as shown in the previous image, in order to add them we will need to use a code fragment that allows us to add this functionality and display the features we want in our store.

So if we want to display this on our website we need to use a code snippet like the following:

add_action( ‘woocommerce_after_shop_loop_item_title’, ‘webempresablog_shop_product_short_description’, 35, 2 ); function webcompanyblog_shop_product_short_description() { the_excerpt(); }

This will allow us to display what we need in our store, but how do we add it safely?

3 methods we have to add snippets in WordPress

Add snippets in WordPress through a child theme.

Starting from the previous case, we already know that we need to add a small additional code that allows us to incorporate what we want. In this sense, the first method that we can use is through a child theme that we have on our website.

Since this article is not about how to create a child theme, if you don’t have one and want to create one, you can review our article that we have available on how to create a child theme.

Once we have our child theme created to add the code we can do it by accessing from our account to wePanel File Manager public_htmhe

Once we are in our public_html we can enter the folder where we have our website and add the code in the file function.php of the child theme that we have installed.

The file function It is generally found in the folder of the theme that we use, it is used to store the functions that allow adding and customizing specific characteristics of a theme as well as expanding its functionalities, it is also used to store functions that can be used by the rest of the file of the theme template.

Once we paste the code in the file function.php From our child theme we just have to save the changes and check the new feature is displayed on our website.

Add snippets in WordPress with the Code Snippets plugin.

Very well we have already seen how to add a code snippet in the child theme of our website, but we can add them with a plugin, the answer to this is yes, there are currently different plugins that allow us to incorporate code into our website without the need for edit any file of our theme or WordPress.

See also  cPanel Disk Usage control the space occupied!

One of the many that are currently used is for its ease and simplicity of adding code snippets to your website, this plugin was developed by the Code Snippets Pro company, the main idea of ​​this plugin is to allow us to add small code snippets of both type PHP as HTML in a simple way and with a few simple steps directly from the administrator of our website, in its pro version it allows you to add other types of code such as CSS, Javascript as well as having direct integration with constructors such as .

To use and see how the plugin works, the first thing is to search for it and .

Once we have it installed we will see that Code Snippets shows us a simple interface that is added to the menu of our website and which allows us to add and manage the code fragments that we are incorporating into our website.

Within the code snippets editor we will see that they include the following fields:

  • 1._ Title: Which helps us to add the name to our code and identify it faster.
  • 2._ Code: Within this section we will have two tabs where we can add the type of code we want, be it PHP or HTML. Keep in mind that usually if we need to add additional features to our website the type of code to use will be PHP.
  • 3.- Option where the code is executed: In this section we will have 4 options where we can decide how we want the code that we are adding to be executed, usually the default option that indicates “Run the code snippet everywhere” is used.
  • 4.- Description: To add a small review or summary for which we add this code.
  • 5.- Labels: Which allow us to categorize this fragment and know how we are using it.

Once we know the plugin a little and how it works to add our code snippet we have to access its options and press add new code snippet from your main menu

Inside the editor we need to add the name with which we will identify our code and paste the code snippet that will allow us to add the functionality to our website.

See also  How to import or export contacts in Horde (Webmail)

In this simple way we can add this functionality that we want to incorporate into our website or some other fragment that code that we need, we will only have to add a new fragment every time we need it.

Add code snippet in WordPress with our own plugin.

Another option that we have available to add a code snippet in WordPress is by creating our own plugin.

Within this plugin we can add all the codes that we need to incorporate into our website, to add a plugin of this type the first thing is to create the folder of our plugin for example: Mycodes and within this the file called mycodes.php where we will incorporate code snippets, the structure this file should have.

This way you will have your custom plugin and have complete control over what you add and install on your website.

You can read in more detail how you can create your own WordPress plugin, you can review the following entry that we have available where it is explained in detail.

Conclusions:

WordPress offers us many themes and plugins that allow us to add features to our website; however sometimes we want to extend these functionalities and improve the content and visualization of our site by creating specific features that you want to add, with these methods or a plugin like code snippets we can quickly and easily add these functionalities that we want without the need to touch or alter the code of our theme.

In addition, this allows us to be sure that in future updates these features that we are adding will not be lost, since our code will not be overwritten.

Check out these additional articles that may interest you and that allow you to customize our website:

Help us improve our content by sharing your opinion

WordPress and Woocommerce support team at .

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