Guide on inserting shortcode in WordPress

Hướng dẫn chèn shortcode vào WordPress

Shortcode is a simplified code in WordPress that executes specific tasks defined in the functions.php file, making it easier and quicker to write and use. To create a shortcode, you first need to initialize its function in functions.php, and then use either a widget or a filter to insert the shortcode into your website. Examples include displaying a greeting message, inserting Youtube videos, or customizing the appearance of your website. Shortcodes are a useful feature in WordPress that can enhance the functionality and design of your website. Explore more WordPress tips and tricks at https://wiki.minhduy.vn/huong-dan-website/huong-dan-wordpress.

What is Shortcode?

Shortcode, when translated, means a short code; it executes the task specified in the functions.php file. Instead of writing long, complicated code, shortcodes make it shorter and easier when called upon. For instance, when displaying articles on the WordPress homepage, we usually use a Loop, so when written, there is only a short paragraph. [shortcode].

How to create a shortcode for WordPress?

To insert a shortcode into WordPress, you must initialize its function in the file functions.php. For example:

You can insert this code at the bottom of the file functions.php:

function tao_shortcode() {
    echo "Xin chào!";
}
add_shortcode( 'vidu_shortcode', 'tao_shortcode' );

Then, write this code into the widget:

[vidu-shortcode]

Instructions for inserting shortcode into WordPress

Or, you can use a Filter:

add_filter('widget_text', 'tao_shortcode');

After saving and loading the website, you will see the word "Hello!" appear, indicating that the shortcode has been successfully created!

To write shortcode into the PHP file, use the code:

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

To write on the WordPress sidebar, use the following code in the functions.php file:

add_filter('widget_text', 'tao_shortcode');

To insert YouTube videos into WordPress, follow these steps:

See also  Flatsome theme article without the need for plugins

Add the code below to the end of the functions.php file:

function tao_youtube_shortcode( $args, $content ) {
    $content="";
    return $content;
}
add_shortcode('youtube', 'tao_youtube_shortcode');

Next, insert the code below into your article to see the results!

Before inserting, remember to press the tab.

Instructions for inserting shortcode into WordPress

Inserting shortcode into WordPress is easy, right? Shortcode has a pretty cool feature when creating a website on WordPress. Besides, you can also learn and use other shortcodes!

Read more interesting knowledge about WordPress tips here.

Rate this post

Related posts