{"id":767,"date":"2008-03-28T12:35:30","date_gmt":"2008-03-28T10:35:30","guid":{"rendered":"http:\/\/planetozh.com\/blog\/2008\/03\/wordpress-25-shortcodes-api-overview\/"},"modified":"2008-03-29T11:44:18","modified_gmt":"2008-03-29T09:44:18","slug":"wordpress-25-shortcodes-api-overview","status":"publish","type":"post","link":"https:\/\/planetozh.com\/blog\/2008\/03\/wordpress-25-shortcodes-api-overview\/","title":{"rendered":"WordPress 2.5 ShortCodes API Overview"},"content":{"rendered":"<p>WordPress 2.5 comes with a very handy API that plugin authors should love: shortcodes, ie. BBcode-like tags, that are parsed when found in post contents. The API is very easy to use, and well done enough that it&#39;s relatively foolproof.<\/p>\n<h2>Shortcode types<\/h2>\n<p>The API allows for all the possible combinations and forms you&#39;d think of: selfclosing or open tags, attributes with single or double quotes.<\/p>\n<div class=\"igsh-code-box\" id=\"ig-sh-1\"><pre class=\"language-none line-numbers\" data-no-optimize=\"1\" data-cfasync=\"false\"><code class=\"language-none\">[tag]\r\n[tag \/]\r\n[tag attribute=&quot;value&quot; \/]\r\n[tag attr1=&#039;value1&#039; attr2=&quot;value2&quot; ]\r\n[tag]enclosed content[\/tag]\r\n[tag attr=&quot;value&quot;]enclosed content[\/tag]<\/code><\/pre><\/div>\n<h2>The Concept<\/h2>\n<p>First, you have to define a function that will take care of your shortcodes, their potential attributes, and any enclosed content:<\/p>\n<div class=\"igsh-code-box\" id=\"ig-sh-2\"><pre class=\"language-php line-numbers\" data-no-optimize=\"1\" data-cfasync=\"false\"><code class=\"language-php\">function my_shortcode_func($attr, $content) {\r\n\t\/\/ $attr is an array of $key=&gt;$value pairs\r\n\t\/\/ $content is a string containing the enclosed content\r\n\t\/\/ ... do something with $attr and $content\r\n\t\/\/ return the expected result\r\n}<\/code><\/pre><\/div>\n<p>Then, to register your new shortcode so that [tag attr=&quot;value&quot;]content[\/tag] is processed as wished by my_shortcode_func(), you just add:<\/p>\n<div class=\"igsh-code-box\" id=\"ig-sh-3\"><pre class=\"language-php line-numbers\" data-no-optimize=\"1\" data-cfasync=\"false\"><code class=\"language-php\">add_shortcode(&#039;tag&#039;, &#039;my_shortcode_func&#039;);<\/code><\/pre><\/div>\n<h2>All API functions<\/h2>\n<p>The API comes with a set of mostly self explanatory functions you can use:<\/p>\n<div class=\"igsh-code-box\" id=\"ig-sh-4\"><pre class=\"language-php line-numbers\" data-no-optimize=\"1\" data-cfasync=\"false\"><code class=\"language-php\">add_shortcode(&#039;tag&#039;, &#039;function_name&#039;); \/\/ register a new shortcode\r\nremove_shortcode(&#039;tag&#039;); \/\/ unregister\r\nremove_all_shortcodes(); \/\/ just as it says\r\n$return = do_shortcode($content); \/\/ apply shortcodes to content without echoing anything<\/code><\/pre><\/div>\n<h2>Examples<\/h2>\n<h3>[tag1] &rarr; Some Longer Text<\/h3>\n<div class=\"igsh-code-box\" id=\"ig-sh-5\"><pre class=\"language-php line-numbers\" data-no-optimize=\"1\" data-cfasync=\"false\"><code class=\"language-php\">function shortcode_example1() {\r\n    return &#039;Some Longer Text&#039;;\r\n}\r\nadd_shortcode(&#039;tag1&#039;, &#039;shortcode_example1&#039;);<\/code><\/pre><\/div>\n<h3>[tag2 param=&quot;something&quot;] &rarr; Some Longer Text and something<\/h3>\n<div class=\"igsh-code-box\" id=\"ig-sh-6\"><pre class=\"language-php line-numbers\" data-no-optimize=\"1\" data-cfasync=\"false\"><code class=\"language-php\">function shortcode_example2($attr) {\r\n    return &#039;Some Longer Text and &#039;.$attr[&#039;param&#039;];\r\n}\r\nadd_shortcode(&#039;tag2&#039;, &#039;shortcode_example2&#039;);<\/code><\/pre><\/div>\n<h3>[tag3]something[\/tag3] &rarr; You just said &quot;something&quot;, didn&#39;t you ?<\/h3>\n<div class=\"igsh-code-box\" id=\"ig-sh-7\"><pre class=\"language-php line-numbers\" data-no-optimize=\"1\" data-cfasync=\"false\"><code class=\"language-php\">function shortcode_example3($attr, $content) {\r\n    return &quot;You just said &#039;$content&#039;, didn&#039;t you ?&quot;;\r\n}\r\nadd_shortcode(&#039;tag3&#039;, &#039;shortcode_example3&#039;);<\/code><\/pre><\/div>\n<h3>[tag4 any attribute]something[\/tag4] &rarr; Attributes: {list of attributes}. Content: {content}<\/h3>\n<div class=\"igsh-code-box\" id=\"ig-sh-8\"><pre class=\"language-php line-numbers\" data-no-optimize=\"1\" data-cfasync=\"false\"><code class=\"language-php\">function shortcode_example4($attr, $content) {\r\n\t$attributes = &#039;&#039;;\r\n\tforeach ($attr as $key=&gt;$value) {\r\n\t\t$attributes .= &quot;$key: $value\\n&quot;;\r\n\t}\r\n    return &quot;Attributes: $attributes&lt;br\/&gt;Content: $content&quot;;\r\n}\r\nadd_shortcode(&#039;tag4&#039;, &#039;shortcode_example4&#039;);<\/code><\/pre><\/div>\n<h2>Experiment and read more<\/h2>\n<p>Test and experiment: all the examples above are gathered into this simple <a href=\"https:\/\/planetozh.com\/blog\/wp-content\/uploads\/2008\/03\/shortcodesphp.txt\" title=\"Shortcodes plugin example\">Shortcodes plugin example<\/a> (rename as shortcodes.php and put in your plugin directory). It&#39;s interesting to try to break things with unexpected attributes or enclosed content: the API is pretty foolproof and should ignore things just as you&#39;d thought it should.<\/p>\n<p>Read more : the whole API can be found in wp-includes\/shortcodes.php and is relatively well commented. Jacob Santos also has a nice and detailed <a href=\"http:\/\/funcdoc.wordpress.com\/2008\/02\/03\/shortcodes-api\/\">overview of the API<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>WordPress 2.5 comes with a very handy API that plugin authors should love: shortcodes, ie. BBcode-like tags, that are parsed when found in post contents. The API is very easy to use, and well done enough that it&#39;s relatively foolproof. Shortcode types The API allows for all the possible combinations and forms you&#39;d think of: selfclosing or open tags, attributes\u2026<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[21],"tags":[2,10,287,245],"class_list":["post-767","post","type-post","status-publish","format-standard","hentry","category-published","tag-code","tag-php","tag-shortcodes","tag-wordpress"],"_links":{"self":[{"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/posts\/767","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/comments?post=767"}],"version-history":[{"count":0,"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/posts\/767\/revisions"}],"wp:attachment":[{"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/media?parent=767"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/categories?post=767"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/tags?post=767"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}