Last active
May 21, 2023 00:32
-
-
Save chrisbergr/fcf39605b5795e8b0d8b78700031e374 to your computer and use it in GitHub Desktop.
WordPress Spoiler BBCode
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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