Guide to Creating Shortcodes: What Is an English Shortcode?

Shortcode là gì? hướng dẫn tạo shortcode

Shortcode is a piece of code that performs pre-set tasks and is commonly used in programming. It can be created in WordPress by writing the necessary code in the theme’s functions.php file. To create a shortcode, one needs to set up a function to execute the code and generate a shortcode name based on the function created. By using the add_shortcode function, the shortcode can be linked to the function to display the desired output. Shortcodes can be used to display specific content, such as random posts, in a more interactive and dynamic way on a WordPress website.

Shortcode is a short piece of code that performs pre-set tasks. This is an extremely useful feature that programmers should know. Let’s join the WordPress course to learn what is shortcode and how to create shortcode.

What is the concept of Shortcode?

Shortcode, translated into Vietnamese, means Short Code or can be understood as a short code. This short code will perform the tasks that you have set up while creating the shortcode. Currently, shortcode is being used quite a lot. You can go to the WordPress plugin library, search for the plugin with the keyword shortcode, and find many plugins to support you. Let’s continue to learn how to create shortcode.

Instructions for creating Shortcode

After understanding what shortcode is, I will guide you on how to create a shortcode. All the code in this article will be written in the theme’s functions.php file. There are two main steps to creating a shortcode:

  • Set the function to execute the code in the shortcode.
  • Generate a shortcode name based on the function created for it.
See also  Error establishing database connection in WordPress

To help you visualize, I will provide a sample of creating shortcode.

function create_shortcode() {
  echo "Hello World!";
}

add_shortcode('test_shortcode', 'create_shortcode');

I have commented the important parts in the code. This means that if you write [test_shortcode] into the content of the article, it will display the phrase "Hello World!" instead of the shortcode.

Instructions for creating Shortcode using parameters

In the above sections, we learned how to create a simple shortcode that only displays what we wrote in the shortcode. To allow users to edit the displayed content, we can use parameters in the shortcode. Here is an example of creating a shortcode that contains parameters.

function create_shortcode_thamso($args, $content) {
    return "This is number " . $args['thamso1'];
}

add_shortcode('shortcode_thamso', 'create_shortcode_thamso');

In the function creation part, we have two parameters: $args and $content. Users can set the value of the parameter in the shortcode. By using parameters, users can customize the displayed content within the shortcode.

Instructions for writing Shortcode into PHP file

Shortcodes are only executed in the WordPress editor. If you want to insert a shortcode into a PHP file, you must use the do_shortcode() function to execute it. For example:

<?php echo do_shortcode('[test_shortcode]'); ?>

Instructions for writing Shortcode into Text Widget

By default, the Text Widget does not allow you to insert shortcode. To enable shortcode execution in Text Widgets, you can filter it by inserting the following code into the functions.php file:

add_filter('widget_text', 'do_shortcode');

We have learned what shortcode is, some ways to create shortcode. Feel free to leave your comments for the WordPress course if you have any questions.

Rate this post

Related posts