Shortcodes is a great way to extend the core functionalities of WordPress core. Nowadays all the best WordPress plugins using shortcodes to integrate their features and options within the pages and posts of sites.
In this tutorial on WordPress do_shortcode(), I will tell you several ways to implement do_shortcode()
function blog_do_shortcode() {
return 'This is a demo do_shortcode message';
}
add_shortcode('do_shortcode', 'blog_do_shortcode');
In this code snippet, blog_do_shortcode is the name of the custom function that implements do_shortcode() functionality into your website. The return statement contains the actual message that needs to be inserted later on.
Finally, in the add_shortcode(), you can see that the exact shortcode to be inserted is “do_shortcode”.
How to Add Parameters to the do_shortcode Function
You already have seen the basic structure of a do_shortcode() function. Now I will tell you how to use it more effectively. In this example, you will learn how to add the dimension of an image to the page.
function parameter_att_do_shortcode($atts) {
extract(shortcode_atts(array(
'width' => 100,
'height' => 150,
), $atts));
return '
';
}
add_shortcode('do_shortcode ', 'parameter_att_do_shortcode');
The function parameter_att_do_shortcode() takes the parameters in the $atts. In order to use the user-provided parameters, I have used shortcode_atts() that takes the user-provided attributes.