Create content shortcodes in WordPress – .com

In this tutorial we will see how to store a value in WordPress so that it can be reused on several sites and automatically updated if we change it.

If you have not understood anything, no problem, let’s give an example. Currently in my there are 4029 classes published. However, I don’t have to write that number (4029) every time, but I have a shortcode that becomes it. Look how this same paragraph looks in the editor:

As you can suppose, this is very comfortable in those cases in which we have certain variables (numbers, words, phrases, images or any other content) that we use repeatedly throughout our website.

Imagine that you have that text in 15 or 20 different places. Can you imagine having to modify it in all of them every time there is a change? The classes of my courses, for example, increase every day. It would be crazy. In your case, perhaps they are not classes, but it is a text, a table, the price of a service or any other variable that may change over time.

So, how can we centralize that value, so that we only have to modify it at a single point? Easy, with shortcodes (sometimes called shortcodes)which allow us to “call” a value, as we have seen in the case of my classes.

Let’s see two approaches to do it. First through a plugin (easier)and then through code (more efficient).

Create content shortcodes (with a plugin)

There are many plugins that allow us to do this kind of thing, but one of the ones I like the most for its simplicity, code quality, and also because it’s free, is . So, everyone to install and activate the plugin.

See also  Figma Course #8. Prototyping and export - .com

Once we have it active, we will see a new option in the administration menu, called “buckets”:

To understand each other, a “bucket” is a “content”. Whatever. A number, a word, an image, a video… whatever we want. In the example of the classes it is only a number, but it could be an illustrated chapter of Don Quixote, if we wanted. There are no limits.

Creating a bucket is very easy and simple. You just have to hit the “Add New” button and you’re done. The familiar interface of a lifetime will appear, similar to that of creating posts, pages or any other content, in which we can put a title and content:

The title is purely informative, and serves for our reference. The content, on the other hand, is the most important thing, since it is what will appear wherever we call the shortcode. In this case, we have put a filler text of “Lorem ipsum dolor site amet…”.

But let’s look at the most important thing, which is what appears below the content field. I mean the section where a shortcode appears:

As you can see, the shortcode has two parameters. The first is the ID, which is required, and the second is the title, which is optional but useful. In any case, this time the complete shortcode is this:

That means that if I put that code in any page, like this:

What we will see if we look at that same page on the web will be this:

So, we already have it done. We can now create our “global values” as buckets and use them wherever we want. By doing it this way, if we modify the bucket itself, we are automatically modifying it in all the places where it appears.

See also  Checkout settings in WooCommerce - .com

In addition, in the case that we create many buckets, we have at our disposal a quick access button to the entire collection, in the toolbar:

As you can see, a very useful plugin that makes our lives much easier if we want to create content centrally and display it repeatedly.

This method may be of interest to those who don’t want to touch code at all, or who have a lot of “little content” to display, but sometimes you don’t need to install a plugin just for that. So let’s see how to do it by code.

Create content shortcodes (by code)

This is my favorite method, because with only three lines of code we save a plugin. Totally recommended if we only have two or three variables that we want to reuse. Let’s see the code:

function jb_shortcode_of_content() {
return ‘This is the content’;
}
add_shortcode(‘code’, ‘jb_shortcode_of_content‘);

As you can see, there are only 3 lines, which do the following:

  1. The first one creates the function “jb_shortcode_de_contenido”.
  2. The second one assigns the value (content) of the shortcode “This is the content”.
  3. The third creates the shortcode and assigns the function to it.

In other words, from now on, every time we write in any post or page, the phrase “This is the content” will actually appear. Needless to say, that phrase can be anything else: the number of classes done, any call to action text, an image, or the Bible in verse. Imagination to the power.

By the way, you must place this code in your functionalities plugin (recommended method) or in your functions.php file (foggy method 😉. If you want to know more about how to create shortcodes, you can take a look at the class, in which you can see through a video how to create basic, complex shortcodes and even with parameters.

See also  Develop an intranet with WordPress - .com

conclusion

If at any time you find yourself manually replacing a value or text that you have written at various points on your websiteand you’ve even seen yourself in that same situation on several occasions, you should consider using a shortcode that replaces that value, so you only need to change it at a single point.

The applications are limitless, the imagination to the power. There are even those who use it to put advertising or announcements that are then easily replaced from a single point. The fact of having everything in a shortcode means that any change can be done easily and quickly.

As always, I hope that the tutorial will be of benefit and usefulness. For any doubt, question or suggestion, you can go to the support section of . See you in the next tutorial!

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