Articles

Free Schema Ready Accordion For Code Snippets

This code snippet creates an accordion menu on WordPress using the “Code Snippets” plugin, enabling users to embed collapsible sections within their content. When the shortcode ( accordion ) is used, it triggers the custom_accordion_shortcode function, which enqueues the jQuery UI Accordion script and applies custom styles to the accordion elements. The styles define the visual appearance, such as background colors, font sizes, and hover effects for the accordion headers and content. The content within the shortcode is processed to wrap each section title (<h3>) and its corresponding content in a div with the class accordion-content. This transformation prepares the content for the accordion functionality.

Additionally, the function generate_json_ld is called within the shortcode function to generate structured data for the content. It scans the content for sections marked by <h3> tags and extracts the text to form FAQ entries. Each FAQ entry includes a question (the text within the <h3> tags) and an answer (the text following each <h3> tag). This structured data is stored globally in $GLOBALS[‘custom_accordion_faqs’]. The structured data is used to enhance search engine results by providing clear and concise information about the FAQ items on the page, potentially leading to better visibility and higher click-through rates in search results.

Finally, the output_json_ld_schema function is hooked to the wp_footer action, ensuring that the structured data is output as JSON-LD script in the footer of the page. This script uses the schema.org format to describe the FAQ content, allowing search engines like Google to understand and present the information effectively in search results. By consolidating all FAQ items on the page, this approach improves SEO performance and provides a better user experience by making the content more accessible and organized.

[code]
//This code snippet creates an accordion menu on WordPress using the “code snippets” plugin. It converts between shortcodes into sections styled with “kadabra-accordion” and then generates schema data for Google to improve search results consolidating all FAQ items on the page.

$GLOBALS[‘custom_accordion_faqs’] = [];
function custom_accordion_shortcode($atts, $content = null) {
wp_enqueue_script(‘jquery-ui-accordion’);
$custom_styles = ‘

‘; // Shortened for brevity
$processed_content = do_shortcode(shortcode_unautop($content));
$processed_content = preg_replace(‘/(

.*?<\/h3>)(.*?)(?=

|$)/si’, ‘$1

$2

‘, $processed_content);
$return = $custom_styles . ‘

‘ . $processed_content . ‘

‘;
generate_json_ld($content);
$return .= ‘‘; // Your accordion script here, shortened for brevity
return $return;
}
function generate_json_ld($content) {
preg_match_all(‘/]*>(.*?)<\/h3>(.*?)(?=

Join the conversation

Live conversation — moderated by AI, free to join, no signup