Skip to content

Instantly share code, notes, and snippets.

@chrisbergr
Last active May 21, 2023 00:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrisbergr/fcf39605b5795e8b0d8b78700031e374 to your computer and use it in GitHub Desktop.
Save chrisbergr/fcf39605b5795e8b0d8b78700031e374 to your computer and use it in GitHub Desktop.
WordPress Spoiler BBCode
<?php
// Thanks to the WordPress Shortcode API it is super easy to implement BBCodes for WordPress sites
// Usage: [SPOILER]Lorem ipsum...[/SPOILER], [SPOILER title="Optional title"]Lorem ipsum...[/SPOILER]
// Make it beautiful with your custom css for .spoiler :)
// The following snippet goes into the functions.php of your theme or child-theme.
add_shortcode( 'SPOILER', 'bb_code_spoiler' );
function bb_code_spoiler( $atts, $content = null ) {
$a = shortcode_atts( array(
'title' => '',
), $atts );
$output = '<details class="spoiler"><summary>%s</summary>%s</details>';
return sprintf(
$output,
'' !== $a['title'] ? 'Spoiler: ' . $a['title'] : 'Spoiler',
wpautop( $content )
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment