{"id":37422,"date":"2019-08-27T12:34:15","date_gmt":"2019-08-27T12:34:15","guid":{"rendered":"https:\/\/wpblog.com\/?p=37422"},"modified":"2019-08-27T13:28:17","modified_gmt":"2019-08-27T13:28:17","slug":"wordpress-do-shortcode","status":"publish","type":"post","link":"https:\/\/wpblog.com\/wordpress-do-shortcode\/","title":{"rendered":"How to Use WordPress do_shortcode"},"content":{"rendered":"<p>For many WordPress developers, shortcodes are a great way of extending the core functionalities of the WordPress core. In fact, all popular plugins now use shortcodes to integrate their features and options within posts and pages of the site.<\/p>\n<div class=\"toc\">\n<h2 class=\"tocp\">Table of Content<\/h2>\n<ol>\n<li><a href=\"#Custom-Message-do-shortcode\">Custom Message do_shortcode<\/a><\/li>\n<li><a href=\"#do_shortcode-Function\">Adding Parameters to the do_shortcode Function<\/a><\/li>\n<li><a href=\"#Build-Custom-do_shortcode-Plugin\">Build a Custom do_shortcode Plugin<\/a><\/li>\n<li><a href=\"#Register-the-Shortcode\">Template to Register the Shortcode<\/a><\/li>\n<\/ol>\n<\/div>\n<p>Now, what if you need to use shortcodes on the website other than the posts and pages. This opens up a host of possibilities for using shortcodes almost anywhere on the website.<\/p>\n<p>In this short tutorial on <strong>WordPress do_shortcode()<\/strong>, I will highlight several ways in which you can leverage this great idea into your website.<\/p>\n<h2 id=\"Custom-Message-do-shortcode\">Custom Message do_shortcode<\/h2>\n<p>A very simple use case of WordPress do_shortcode() is adding a custom message <strong>&#8220;A simple do shortcode demo&#8221;<\/strong> at the location of your choice. The following snippet inserts the message or keyword at the location where the shortcode is placed.<\/p>\n<pre class=\"lang:default decode:true\">function wp_do_shortcode() {\r\nreturn 'A simple do shortcode demo';\r\n}\r\nadd_shortcode('do_shortcode', 'wp_do_shortcode');<\/pre>\n<p>In the above code snippet, <strong>wp_do_shortcode<\/strong> is the name of the custom function that integrates <strong>do_shortcode()<\/strong> functionality into your website. The return statement contains the actual message or keyword that needs to be inserted.<\/p>\n<p>Finally, in the <strong>add_shortcode()<\/strong>, you can see that the actual shortcode to be inserted is \u201c<strong>do_shortcode<\/strong>\u201d.<\/p>\n<p>As you can see in the following screenshots, when the shortcode, do_shortcode is used:<\/p>\n<p><img loading=\"lazy\" class=\"alignnone size-full wp-image-37423\" src=\"https:\/\/wpblog.com\/wp-content\/uploads\/2019\/08\/WordPress-do_shortcode.jpg\" alt=\"WordPress do_shortcode\" width=\"1264\" height=\"262\" srcset=\"https:\/\/wpblog.com\/wp-content\/uploads\/2019\/08\/WordPress-do_shortcode.jpg 1264w, https:\/\/wpblog.com\/wp-content\/uploads\/2019\/08\/WordPress-do_shortcode-300x62.jpg 300w, https:\/\/wpblog.com\/wp-content\/uploads\/2019\/08\/WordPress-do_shortcode-768x159.jpg 768w, https:\/\/wpblog.com\/wp-content\/uploads\/2019\/08\/WordPress-do_shortcode-1024x212.jpg 1024w\" sizes=\"(max-width: 1264px) 100vw, 1264px\" \/><\/p>\n<p>You can see that the shortcode gets replaced by the message in the return statement.<\/p>\n<p><img loading=\"lazy\" class=\"size-full aligncenter\" src=\"https:\/\/wpblog.com\/wp-content\/uploads\/2018\/10\/Integration-of-do_shortcode.jpg\" alt=\"Integration of do_shortcode\" width=\"555\" height=\"237\" \/><\/p>\n<h2 id=\"do_shortcode-Function\">Adding Parameters to the do_shortcode Function<\/h2>\n<p>Now that you know the basic structure of a do_shortcode() and how to use it in your code, I will present another example that allows you to use the idea more effectively. In this example, I will demonstrate how to add the dimensions of an image to the page. For this, check out the following code:<\/p>\n<pre class=\"lang:default decode:true\">function parameter_att_do_shortcode($atts) {\r\nextract(shortcode_atts(array(\r\n'width' =&gt; 100,\r\n'height' =&gt; 150,\r\n), $atts));\r\nreturn '&lt;img src=\"https:\/\/abc.com\/'. $width . '\/'. $height . '\" \/&gt;';\r\n}\r\n\r\nadd_shortcode('do_shortcode ', 'parameter_att_do_shortcode');<\/pre>\n<p>As you can see, the <strong>function parameter_att_do_shortcode()<\/strong> takes the parameters in the <strong>$atts<\/strong>. In order to make use of the user-provided parameters, I have used <strong>shortcode_atts()<\/strong> that takes in user-provided attributes and fill in the gaps with default arguments.<\/p>\n<h2 id=\"Build-Custom-do_shortcode-Plugin\">Build a Custom do_shortcode Plugin<\/h2>\n<p>If you wish to create a plugin to add do_shortcode functionality to your website, the process is pretty straightforward.<\/p>\n<p>The process of creating the plugin is simple. Just create a new folder in the <strong>wp-cont\/Plugins<\/strong>. in the <strong>new folder<\/strong>, create a file named <strong>plugin-name-seokeyword.php<\/strong>. open the file and add the following code snippet to it.<\/p>\n<pre class=\"lang:default decode:true\">&lt;?php\r\n\/*\r\nPlugin Name: seo suggested do shortcode Integration Plugin \r\nDescription: add a description with seo suggest keyword This plugin extends the default do_shortcode functunality and makes shortcodes available across all widgetized areas\r\nVersion: 1.0\r\nAuthor: Muhammad Owais Alam\r\n*\/\r\nfunction wp_do_shortcode() {\r\nreturn 'A simple do shortcode demo';\r\n}\r\nadd_shortcode('do_shortcode', 'wp_do_shortcode');\r\n\r\n\r\nfunction parameter_att_do_shortcode($atts) {\r\nextract(shortcode_atts(array(\r\n'width' =&gt; 100,\r\n'height' =&gt; 150,\r\n), $atts));\r\nreturn '&lt;img src=\"https:\/\/abc.com\/'. $width . '\/'. $height . '\" \/&gt;';\r\n}\r\n\r\nadd_shortcode('do_shortcode ', 'parameter_att_do_shortcode');<\/pre>\n<p><img loading=\"lazy\" class=\"size-full wp-image-37498 aligncenter\" src=\"https:\/\/wpblog.com\/wp-content\/uploads\/2019\/08\/do_shortcode-Integration-plugin.jpg\" alt=\"do_shortcode Integration plugin\" width=\"941\" height=\"68\" srcset=\"https:\/\/wpblog.com\/wp-content\/uploads\/2019\/08\/do_shortcode-Integration-plugin.jpg 941w, https:\/\/wpblog.com\/wp-content\/uploads\/2019\/08\/do_shortcode-Integration-plugin-300x22.jpg 300w, https:\/\/wpblog.com\/wp-content\/uploads\/2019\/08\/do_shortcode-Integration-plugin-768x55.jpg 768w\" sizes=\"(max-width: 941px) 100vw, 941px\" \/><\/p>\n<h2 id=\"Register-the-Shortcode\">Template to Register the Shortcode<\/h2>\n<p>Before using the shortcode plugin, you need to register the shortcode. For this, you need to create a separate file with the following <strong>function wordpress_do_shortcode_form()<\/strong>.<\/p>\n<pre class=\"lang:default decode:true\">function wordpress_do_shortcode_form() {\r\nob_start();\r\nget_template_part('template-name');\r\nreturn ob_get_clean();\r\n}\r\nadd_shortcode( 'wordpress_do_shortcode_form', 'wordpress_do_shortcode_form' );<\/pre>\n<h2>Wrapping Up<\/h2>\n<p>Shortcodes are a popular way of adding functionalities to the WordPress core. With the above-mentioned solution, you can extend the usability and application of shortcodes to all areas of the WordPress website. If you need help in implementing the idea at your site, do let me know in the comments.<\/p>\n<!-- AddThis Advanced Settings generic via filter on the_content --><!-- AddThis Share Buttons generic via filter on the_content -->","protected":false},"excerpt":{"rendered":"<p>For many WordPress developers, shortcodes are a great way of extending the core functionalities of the WordPress core. In fact, all popular plugins now use shortcodes to integrate their features and options within posts and pages of the site. Table of Content Custom Message do_shortcode&#8230;<!-- AddThis Advanced Settings generic via filter on get_the_excerpt --><!-- AddThis Share Buttons generic via filter on get_the_excerpt --><\/p>\n","protected":false},"author":28,"featured_media":37428,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[47],"tags":[1017,1019,1018],"acf":[],"_links":{"self":[{"href":"https:\/\/wpblog.com\/wp-json\/wp\/v2\/posts\/37422"}],"collection":[{"href":"https:\/\/wpblog.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wpblog.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wpblog.com\/wp-json\/wp\/v2\/users\/28"}],"replies":[{"embeddable":true,"href":"https:\/\/wpblog.com\/wp-json\/wp\/v2\/comments?post=37422"}],"version-history":[{"count":8,"href":"https:\/\/wpblog.com\/wp-json\/wp\/v2\/posts\/37422\/revisions"}],"predecessor-version":[{"id":37500,"href":"https:\/\/wpblog.com\/wp-json\/wp\/v2\/posts\/37422\/revisions\/37500"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/wpblog.com\/wp-json\/wp\/v2\/media\/37428"}],"wp:attachment":[{"href":"https:\/\/wpblog.com\/wp-json\/wp\/v2\/media?parent=37422"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wpblog.com\/wp-json\/wp\/v2\/categories?post=37422"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wpblog.com\/wp-json\/wp\/v2\/tags?post=37422"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}