Deprecated: pathinfo(): Passing null to parameter #1 ($path) of type string is deprecated in /home/indithem/public_html/wp-content/plugins/urvanov-syntax-highlighter/class-urvanov-syntax-highlighter-langs.php on line 86
Deprecated: pathinfo(): Passing null to parameter #1 ($path) of type string is deprecated in /home/indithem/public_html/wp-content/plugins/urvanov-syntax-highlighter/class-urvanov-syntax-highlighter-langs.php on line 86
Deprecated: pathinfo(): Passing null to parameter #1 ($path) of type string is deprecated in /home/indithem/public_html/wp-content/plugins/urvanov-syntax-highlighter/class-urvanov-syntax-highlighter-langs.php on line 86
WordPress comes equipped with a pretty neat feature known as Shortcode. The most basic function of a Shortcode is to output certain HTML content without having to call functions or call any actions. With the help of Shortcodes, we can insert custom HTML content anywhere in a document especially within a post’s content. Take a look at the code below-
1 2 3 4 5 6 7 8 9 |
function my_theme_custom_function() { $output .= '123'; return $output; } add_shortcode('my_theme_custom_shortcode', 'my_theme_custom_function'); |
Here, we define a simple function to return ‘123’. Keep in mind that whenever we want to create a shortcode, we must make it in the form of a string and then the string must be returned. This is the basic concept of a shortcode. Now, after the function, we define the shortcode my_theme_custom_shortcode
using the function add_shortcode
. This shortcode can be called anywhere in the content of a post or page by enclosing it in square brackets.
1 2 3 |
[my_theme_custom_shortcode] // Outputs '123' in the post content |
Now, if we want to call it like a function in a PHP file, WordPress provides another function do_shortcode
. Consider the following code –
1 2 3 |
<?php echo do_shortcode('[my_theme_custom_shortcode]'); ?> |
Here, a shortcode is called just like a normal function with the help of the do_shortcode
function. Keep in mind that we need to echo the output of the do_shortcode
function as the output is only returned in a shortcode( remember? ).
Shortcodes were always a part of the WordPress ecosystem but with the advent of the Block Editor, they have really proven to be a life-saver. The Block Editor, with all the interactivity and reactivity, is still evolving and in turn, is not that flexible. Some very useful layouts (the coveted 4-Column layout) are not directly possible in the Block Editor. Luckily, WordPress does have a Shortcode block through which we can insert showcases whenever and wherever we want in the content.
Shortcodes are a very powerful tool in the WordPress arsenal and lend a great deal of flexibility to WordPress. And with the advent of block-editor, they have proven to be a life-saver as block-editor sometimes is just not enough.
I hope you got to learn something from this little WordPress tip. For more tips, tricks and tutorials related to WordPress, do check out our blog. We posted useful articles on s regular basis to take your WordPress game to the next level.