{"id":227,"date":"2025-04-01T11:47:27","date_gmt":"2025-04-01T11:47:27","guid":{"rendered":"https:\/\/www.kwwd.co.uk\/blog\/?p=227"},"modified":"2025-04-01T11:47:27","modified_gmt":"2025-04-01T11:47:27","slug":"wordpress-spoiler-in-post-content-function","status":"publish","type":"post","link":"https:\/\/www.kwwd.co.uk\/blog\/wordpress-spoiler-in-post-content-function\/","title":{"rendered":"WordPress: Spoiler In Post Content Function"},"content":{"rendered":"<p>The following code can be added to your WordPress functions.php file or added as a plugin to allow you to mark sections of\u00a0 your post content as &#8220;Spoilers&#8221;:<\/p>\n<pre><code class=\"language-php line-numbers\">\r\nfunction spoiler_shortcode($atts, $content = null) {\r\n    \/\/ Extract shortcode attributes\r\n    $atts = shortcode_atts(\r\n        array(\r\n            'title' =&gt; 'Spoiler',\r\n        ),\r\n        $atts,\r\n        'spoiler'\r\n    );\r\n\r\n    \/\/ Generate a unique identifier for this spoiler\r\n    $spoiler_id = uniqid('spoiler_');\r\n\r\n    \/\/ Output the HTML structure for the spoiler\r\n    $output = '&lt;div class=\"spoiler\" id=\"' . esc_attr($spoiler_id) . '\"&gt;';\r\n    $output .= '&lt;div class=\"spoiler-title\"&gt;' . esc_html($atts['title']) . '&lt;\/div&gt;';\r\n    $output .= '&lt;div class=\"spoiler-content\"&gt;' . wp_kses_post($content) . '&lt;\/div&gt;';\r\n    $output .= '&lt;\/div&gt;';\r\n\r\n    \/\/ Enqueue the necessary scripts\r\n    add_action('wp_footer', 'spoiler_enqueue_scripts');\r\n\r\n    return $output;\r\n}\r\n\r\nfunction spoiler_enqueue_scripts() {\r\n    ?&gt;\r\n    &lt;script&gt;\r\n        document.addEventListener('DOMContentLoaded', function () {\r\n            var spoilers = document.querySelectorAll('.spoiler');\r\n\r\n            spoilers.forEach(function (spoiler) {\r\n                spoiler.addEventListener('click', function () {\r\n                    spoiler.classList.toggle('revealed');\r\n                });\r\n            });\r\n        });\r\n    &lt;\/script&gt;\r\n\r\n    &lt;style&gt;\r\n        .spoiler {\r\n            border: 1px solid #000;\r\n            padding: 10px;\r\n            margin-bottom: 10px;\r\n            cursor: pointer;\r\n        }\r\n\r\n        .spoiler-title {\r\n            font-weight: bold;\r\n            margin-bottom: 5px;\r\n        }\r\n\r\n        .spoiler-content {\r\n            filter: blur(5px);\r\n            transition: filter 0.3s ease;\r\n        }\r\n\r\n        .spoiler.revealed .spoiler-content {\r\n            filter: none;\r\n        }\r\n    &lt;\/style&gt;\r\n    &lt;?php\r\n}\r\n\r\n\/\/ Register the shortcode\r\nadd_shortcode('spoiler', 'spoiler_shortcode');\r\n\r\n<\/code><\/pre>\n<p>Usage:<\/p>\n<pre><code class=\"language-html\">[spoiler title=\"Spoiler Title\"]Spoiler Text here - this will be blurred until it's clicked on[\/spoiler]<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>The following code can be added to your WordPress functions.php file or added as a plugin to allow you to mark sections of\u00a0 your post [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-227","post","type-post","status-publish","format-standard","hentry","category-wordpress"],"_links":{"self":[{"href":"https:\/\/www.kwwd.co.uk\/blog\/wp-json\/wp\/v2\/posts\/227","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.kwwd.co.uk\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.kwwd.co.uk\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.kwwd.co.uk\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.kwwd.co.uk\/blog\/wp-json\/wp\/v2\/comments?post=227"}],"version-history":[{"count":0,"href":"https:\/\/www.kwwd.co.uk\/blog\/wp-json\/wp\/v2\/posts\/227\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.kwwd.co.uk\/blog\/wp-json\/wp\/v2\/media?parent=227"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kwwd.co.uk\/blog\/wp-json\/wp\/v2\/categories?post=227"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kwwd.co.uk\/blog\/wp-json\/wp\/v2\/tags?post=227"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}