The shortcode gets registered the same as before with add_shortcode(). The attributes are added to the shortcode very much like they would be added to an HTML element: <\/p>\n
[js_alert_box title=\"Attention\" content=\"Check out this alert box\"]<\/pre>\n <\/p>\n
Calling the shortcode with these attributes would render this content on the page:<\/p>\n
<div class=\"alert\">\r\n<h2>Attention<\/h2>\r\nCheck out this alert box\r\n<\/div><\/pre>\n <\/p>\n
Adding Shortcode Content<\/h3>\n
Shortcodes can be used like HTML tags to wrap content and pass the content to the shortcode. This allows a user to add more than a few words to the shortcode and use punctuation without escaping. To allow this, add a second argument to the alert box function called $content, which will be the content that is wrapped with the shortcode tags. It might be nice to use this to allow users to add content to the alert box in our example, this is what js_alert_box_function() would look like to add the content:<\/p>\n
\/**\r\n * render alert box\r\n * [js_alert_box]\r\n *\r\n * @author Joe Sexton <joe@webtipblog.com>\r\n * @param array $atts\r\n * @param string $content\r\n * @return string\r\n *\/\r\nfunction js_alert_box_function( $atts, $content = null ) {\r\n\r\n extract( shortcode_atts( array(\r\n 'title' => null,\r\n ), $atts ) );\r\n\r\n $output = '<div class=\"alert\">';\r\n if ( !is_null( $title ) ) {\r\n $output .= '<h2>'.$title.'<\/h2>';\r\n }\r\n $output .= $content.'<\/div>';\r\n\r\n return $output;\r\n}<\/pre>\n <\/p>\n
The shortcode gets registered normally using add_shortcode() and calling the shortcode with content would look like this:<\/p>\n
[js_alert_box title=\"Attention\"]Check out this alert box[\/js_alert_box]<\/pre>\n <\/p>\n
This will render the following on the page:<\/p>\n
<div class=\"alert\">\r\n<h2>Attention<\/h2>\r\nCheck out this alert box\r\n<\/div><\/pre>\n <\/p>\n
Separate Shortcode Template Files<\/h3>\n
If the shortcode will be returning more than a few lines of HTML it might be a good idea to use a separate template file for the shortcode template. Since the shortcode content is returned and not simply printed we need to do more than just require() the template file. By using output buffering the template file can be added as a separate file and its contents returned instead of printed. To add a separate template file for the alert box shortcode, update js_alert_box_function() to include output buffering:<\/p>\n
\/**\r\n * render alert box\r\n * [js_alert_box]\r\n *\r\n * @author Joe Sexton <joe@webtipblog.com>\r\n * @param array $atts\r\n * @param string $content\r\n * @return string\r\n *\/\r\nfunction js_alert_box_function( $atts, $content = null ) {\r\n\r\n extract( shortcode_atts( array(\r\n 'title' => null,\r\n ), $atts ) );\r\n\r\n ob_start();\r\n require '\/templates\/alert_box_template.php';\r\n $output = ob_get_contents();\r\n ob_end_clean();\r\n\r\n return $output;\r\n}<\/pre>\n <\/p>\n
The template file can now be created like any other theme template file using the variables set as shortcode attributes.<\/p>\n
<?php \/\/ \/templates\/alert_box_template.php ?>\r\n\r\n<div class=\"alert\">\r\n\r\n <?php if ( !is_null( $title ) ) : ?>\r\n <h2><?php echo $title; ?><\/h2>\r\n <?php endif; ?>\r\n\r\n <?php echo $content; ?>\r\n<\/div><\/pre>\n","protected":false},"excerpt":{"rendered":"The WordPress Shortcode API allows you to render template blocks by simply calling the shortcode in a post or page. Template blocks can be a part of a theme or a plugin, but may be included by entering a shortcode for the template when creating a post\/page in wp-admin, allowing users to easily include content.<\/p>\n","protected":false},"author":1,"featured_media":313,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[2],"tags":[23,10],"_links":{"self":[{"href":"https:\/\/www.webtipblog.com\/wp-json\/wp\/v2\/posts\/169"}],"collection":[{"href":"https:\/\/www.webtipblog.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.webtipblog.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.webtipblog.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.webtipblog.com\/wp-json\/wp\/v2\/comments?post=169"}],"version-history":[{"count":6,"href":"https:\/\/www.webtipblog.com\/wp-json\/wp\/v2\/posts\/169\/revisions"}],"predecessor-version":[{"id":314,"href":"https:\/\/www.webtipblog.com\/wp-json\/wp\/v2\/posts\/169\/revisions\/314"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.webtipblog.com\/wp-json\/wp\/v2\/media\/313"}],"wp:attachment":[{"href":"https:\/\/www.webtipblog.com\/wp-json\/wp\/v2\/media?parent=169"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.webtipblog.com\/wp-json\/wp\/v2\/categories?post=169"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.webtipblog.com\/wp-json\/wp\/v2\/tags?post=169"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}