{"id":33895,"date":"2019-03-01T16:32:18","date_gmt":"2019-03-01T16:32:18","guid":{"rendered":"http:\/\/getshortcodes.com\/?post_type=docs&#038;p=33895"},"modified":"2019-05-28T13:57:38","modified_gmt":"2019-05-28T13:57:38","slug":"using-shortcodes-in-template-files","status":"publish","type":"docs","link":"https:\/\/getshortcodes.com\/docs\/using-shortcodes-in-template-files\/","title":{"rendered":"Using shortcodes in template files"},"content":{"rendered":"\n<p>You can use shortcodes in template files. To do this you should use a special WordPress function\u00a0<em>do_shortcode()<\/em>.<\/p>\n\n\n<p class=\"h2\">Table of contents<\/p>\n\n<ol><li><a href=\"#simple-example-of-use\">Simple example of use<\/a><\/li><li><a href=\"#shortcodes-with-attributes\">Shortcodes with attributes&nbsp;<\/a><\/li><li><a href=\"#use-of-variables\">Use of variables&nbsp;<\/a><\/li><li><a href=\"#nested-shortcodes\">Nested shortcodes<\/a><\/li><\/ol><\/li><\/ol>\n\n\n<h2 class=\"wp-block-heading\" id=\"simple-example-of-use\">Simple example of use<\/h2>\n\n\n\n<p>This is the simplest example of using&nbsp;<em>do_shortcode()<\/em>&nbsp;function.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">echo do_shortcode( '' );<br><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"shortcodes-with-attributes\">Shortcodes with attributes&nbsp;<\/h2>\n\n\n\n<p><em>do_shortcode()<\/em>&nbsp;function works with shortcode attributes.<\/p>\n\n\n\n<p>Please note the different quotation marks (&#8220;double&#8221; and &#8216;single&#8217;) used for the line with shortcode and for the shortcode attribute&#8217;s meaning. Example of shortcode with attributes:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">echo do_shortcode( '' );<br><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"use-of-variables\">Use of variables&nbsp;<\/h2>\n\n\n\n<p>You can write shortcodes to the variable before calling&nbsp;<em>do_shortcode()<\/em>&nbsp;function. Here are some examples:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$content = 'Hello';<br>$my_shortcode = '[box]' . $content . '[\/box]';<br>echo do_shortcode( $my_shortcode );<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">$color = '#ffcc00';<br>$content = 'Hello';<br>echo do_shortcode( '[box color=\"{$color}\"] {$content} [\/box]' );<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"nested-shortcodes\">Nested shortcodes<\/h2>\n\n\n\n<p><em>do_shortcode()<\/em>&nbsp;function allows you to use nested shortcodes as well. Here are some examples:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">echo do_shortcode( '[box] Hello, [username] [\/box]' );<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">$content = 'Hello, [username]';<br>$shortcode = '[box]' . $content . '[\/box]';<br>echo do_shortcode( $shortcode );<\/pre>\n\n\n\n<p>Important: make sure that opening and closing shortcode tags are used within a single&nbsp;<code>do_shortcode()<\/code>&nbsp;call. See examples below.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\/\/ This code WON'T work<br>echo do_shortcode( '<div class=\"su-box su-box-style-default\" id=\"\" style=\"border-color:#000000;border-radius:3px;\"><div class=\"su-box-title\" style=\"background-color:#333333;color:#FFFFFF;border-top-left-radius:1px;border-top-right-radius:1px\">This is box title<\/div><div class=\"su-box-content su-u-clearfix su-u-trim\" style=\"border-bottom-left-radius:1px;border-bottom-right-radius:1px\">&#8216; );<br>echo do_shortcode( &#8216;<\/div><\/div>' );<br><br>\/\/ This code WILL work<br>$my_shortcode = '<div class=\"su-box su-box-style-default\" id=\"\" style=\"border-color:#000000;border-radius:3px;\"><div class=\"su-box-title\" style=\"background-color:#333333;color:#FFFFFF;border-top-left-radius:1px;border-top-right-radius:1px\">This is box title<\/div><div class=\"su-box-content su-u-clearfix su-u-trim\" style=\"border-bottom-left-radius:1px;border-bottom-right-radius:1px\">&#8216;;<br>$my_shortcode .= &#8216;Box content&#8217;;<br>$my_shortcode .= &#8216;<\/div><\/div>';<br>echo do_shortcode( $my_shortcode );<\/pre>\n","protected":false},"featured_media":0,"template":"","docs_category":[20],"class_list":["post-33895","docs","type-docs","status-publish","hentry","docs_category-getting-started"],"_links":{"self":[{"href":"https:\/\/getshortcodes.com\/api\/wp\/v2\/docs\/33895","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/getshortcodes.com\/api\/wp\/v2\/docs"}],"about":[{"href":"https:\/\/getshortcodes.com\/api\/wp\/v2\/types\/docs"}],"wp:attachment":[{"href":"https:\/\/getshortcodes.com\/api\/wp\/v2\/media?parent=33895"}],"wp:term":[{"taxonomy":"docs_category","embeddable":true,"href":"https:\/\/getshortcodes.com\/api\/wp\/v2\/docs_category?post=33895"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}