{"id":1916,"date":"2020-09-16T06:46:41","date_gmt":"2020-09-15T22:46:41","guid":{"rendered":"https:\/\/www.intelliwolf.com\/?p=1916"},"modified":"2020-09-16T06:46:43","modified_gmt":"2020-09-15T22:46:43","slug":"how-to-make-a-web-accessible-disclosure-toggle-using-shortcodes","status":"publish","type":"post","link":"https:\/\/www.intelliwolf.com\/how-to-make-a-web-accessible-disclosure-toggle-using-shortcodes\/","title":{"rendered":"How To Make A Web Accessible Disclosure Toggle Using Shortcodes"},"content":{"rendered":"\n
I spoke previously about how to make Divi's toggle module web accessible<\/a>, but what if you weren't using Divi?<\/p>\n\n\n\n I needed to have a disclosure pattern toggle<\/a> independent of any theme. It needed to be setup using a shortcode with customizable content.<\/p>\n\n\n\n Ideally, you'd want to wrap this into a plugin, but I'm going to build it inside of a child theme for this tutorial. See the tutorial on child themes<\/a> if you need a refresher.<\/p>\n\n\n\n How to make a web accessible disclosure toggle<\/strong>: In functions.php<\/em> add a shortcode that set the customizable content into a span for the toggle and a div for the content. Add the jQuery to trigger the opening and closing functionality. Add CSS to style.css<\/em> to display the disclosure indicator and handle the visibility.<\/p>\n\n\n\n The code that goes into functions.php<\/em> is:<\/p>\n\n\n\n Then add this to style.css<\/em> or to Additional CSS<\/em> in the Theme Customizer:<\/p>\n\n\n\n For the disclosure indicators, I'm using Font Awesome with the free Font Awesome Plugin<\/a>. You can just install and activate that and be up and running.<\/p>\n\n\n\n If you're using a different font set to display the disclosure indicators, or your theme already has Font Awesome, you might need to adjust the name of the font-family<\/em> or the content<\/em> to match.<\/p>\n\n\n\n To use this shortcode, add the following to your page, post or widget:<\/p>\n\n\n\n The order of the id<\/em> and title<\/em> don't matter.<\/p>\n\n\n\n If you only have one disclosure toggle on a page, you don't need to add the id<\/em>. But if you are going to have multiple disclosure toggles, you need to add a unique id to each one. You can use digits, just not as the first character of the id<\/em>.<\/p>\n\n\n\n The title<\/em> will become the title of the button that triggers the visibility.<\/p>\n\n\n\n The part between the square brackets is the content that will be hidden initially and become visible when someone clicks on it.<\/p>\n\n\n\n The outputted code looks like this:<\/p>\n\n\n\n If you're going to use multiple toggles on a page, then you should pull in the jQuery separately. Otherwise it will be outputted as many times as you have shortcodes and may cause issues.<\/p>\n\n\n\n Instead of the previous, add this code to functions.php<\/em>:<\/p>\n\n\n\n Then you'll need to add the jQuery separately.<\/p>\n\n\n\n If you're going to be using the toggles on multiple pages, add the jQuery to an external file called from your functions.php<\/em> file. Otherwise it's fine to paste it into the page through your page builder or using something like SOGO Header Footer Scripts<\/a>.<\/p>\n\n\n\n This is the jQuery you'll need:<\/p>\n\n\n\n Because this code pulls in the ID of the clicked toggle (the var relatedContent<\/em> line), it can be used with multiple toggles on the same page.<\/p>\n","protected":false},"excerpt":{"rendered":" I spoke previously about how to make Divi’s toggle module web accessible, but what if you weren’t using Divi? I needed to have a disclosure pattern toggle independent of any theme. It needed to be setup using a shortcode with customizable content. Ideally, you’d want to wrap this into a plugin, but I’m going to<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"yoast_head":"\nadd_shortcode( 'mini_toggle', 'mini_toggle_func' );\n\nfunction mini_toggle_func( $atts, $content = \"\" ) {\n $id = !empty( $atts['id'] ) ? $atts['id'] : 'mini-toggle';\n $title = !empty( $atts['title'] ) ? $atts['title'] : 'View more';\n $content_id = $id . '-content';\n $html = \"\";\n $html .= \"<span class='mini-toggle-trigger mini-toggle-closed' role='button' id='$id' aria-controls='$content_id' aria-expanded='false'>$title<\/span>\";\n $html .= \"<div class='mini-toggle-content mini-toggle-closed' id='$content_id' aria-labelledby='$id'><p>$content<\/p><\/div>\";\n $html .=\"<script>\n jQuery(document).ready(function ($) {\n var toggleClosed = 'mini-toggle-closed';\n var toggleOpen = 'mini-toggle-open';\n $('.mini-toggle-trigger').click(function() {\n var relatedContent = $(this).attr('aria-controls');\n if ($(this).hasClass(toggleClosed)) {\n $(this).removeClass(toggleClosed);\n $(this).addClass(toggleOpen);\n $(this).attr('aria-expanded','true');\n $('#' + relatedContent).removeClass(toggleClosed);\n $('#' + relatedContent).addClass(toggleOpen);\n } else {\n $(this).removeClass(toggleOpen);\n $(this).addClass(toggleClosed);\n $(this).attr('aria-expanded','false');\n $('#' + relatedContent).removeClass(toggleOpen);\n $('#' + relatedContent).addClass(toggleClosed);\n }\n });\n });\n <\/script>\";\n return $html;\n}<\/code><\/pre>\n\n\n\n
.mini-toggle-closed.mini-toggle-content {\n display: none;\n}\n.mini-toggle-open.mini-toggle-content {\n display: block;\n}\n.mini-toggle-closed.mini-toggle-trigger::before {\n font-family: \"FontAwesome\";\n content: \"\\f054\";\n}\n.mini-toggle-open.mini-toggle-trigger::before {\n font-family: \"FontAwesome\";\n content: \"\\f078\";\n}<\/code><\/pre>\n\n\n\n
How to use the shortcode<\/h2>\n\n\n\n
[mini_toggle id=\"toggle1\" title=\"See more about us\"]This is the content about us.[\/mini_toggle]<\/code><\/pre>\n\n\n\n
<span class=\"mini-toggle-trigger mini-toggle-closed\" role=\"button\" id=\"toggle1\" aria-controls=\"toggle1-content\" aria-expanded=\"false\">See more about us<\/span>\n<div class=\"mini-toggle-content mini-toggle-closed\" id=\"toggle1-content\" aria-labelledby=\"toggle1\">\n <p>This is the content about us.<\/p>\n<\/div><\/code><\/pre>\n\n\n\n
How to use the code for multiple toggles on a page<\/h2>\n\n\n\n
add_shortcode( 'mini_toggle', 'mini_toggle_func' );\r\n\r\nfunction mini_toggle_func( $atts, $content = \"\" ) {\r\n $id = !empty( $atts['id'] ) ? $atts['id'] : 'mini-toggle';\r\n $title = !empty( $atts['title'] ) ? $atts['title'] : 'View more';\r\n $content_id = $id . '-content';\r\n $html = \"\";\r\n $html .= \"<span class='mini-toggle-trigger mini-toggle-closed' role='button' id='$id' aria-controls='$content_id' aria-expanded='false'>$title<\/span>\";\r\n $html .= \"<div class='mini-toggle-content mini-toggle-closed' id='$content_id' aria-labelledby='$id'><p>$content<\/p><\/div>\";\r\n return $html;\r\n}<\/code><\/pre>\n\n\n\n
<script>\njQuery(document).ready(function ($) {\n var toggleClosed = 'mini-toggle-closed';\n var toggleOpen = 'mini-toggle-open';\n $('.mini-toggle-trigger').click(function() {\n var relatedContent = $(this).attr('aria-controls');\n if ($(this).hasClass(toggleClosed)) {\n $(this).removeClass(toggleClosed);\n $(this).addClass(toggleOpen);\n $(this).attr('aria-expanded','true');\n $('#' + relatedContent).removeClass(toggleClosed);\n $('#' + relatedContent).addClass(toggleOpen);\n } else {\n $(this).removeClass(toggleOpen);\n $(this).addClass(toggleClosed);\n $(this).attr('aria-expanded','false');\n $('#' + relatedContent).removeClass(toggleOpen);\n $('#' + relatedContent).addClass(toggleClosed);\n }\n });\n});\n<\/script><\/code><\/pre>\n\n\n\n