Create any shortcode via function.php . file

What are shortcodes?

Shortcode Translated according to Vietnamese, it means Short Code or in other words a short piece of code. This short code will perform certain actions that you specified while creating the shortcode, such as displaying a Loop. You can execute this shortcode anywhere like in the article, in the theme, except excerpt and widget, but I will show you how to fix it.

Create any shortcode via function.php . file

Create any shortcode via function.php . file

Create any shortcode via function.php . file

All the code in this article you write in the file functions.php of the theme (Recommend to use child theme)

To Create any shortcode via function.php file we will include 2 main steps:

  1. Set up the function to execute the code in the shortcode.
  2. Generate a shortcode name based on the function created for it.

To make it easier to imagine, I give a shortcode creation template like this.

//Initialize function for shortcode //Create shortcode
function shortcode_info()
{ return ‘This is a short message’; }
add_shortcode( 'short1', 'shortcode_info' );

How to write shortcode in PHP file

The shortcode only executes in the WordPress editor, but in other situations it doesn’t understand. So if you want to insert shortcode into a PHP file, you must use the do_shortcode() function for it to execute. Examples are as follows:

How to write shortcode in Text widget

By default, the Text widget will not let you insert the shortcode

[short1]

I wish you success!

0 Comments

(code-box)