Hey everyone,
I’ve been using wpForo for a while now, and one thing I always wished it had was the ability to hide spoilers unless members are actually participating in the forum. So, I decided to crack open the ol’ PHP book and put something together.
It works perfectly for my needs, but you can easily tweak it to fit yours. Here’s how to set it up:
Step 1: Install Code Snippets
-
Install and activate the plugin Code Snippets.
-
In your WordPress dashboard, go to Snippets → Add New.
-
Give it a name you’ll remember (I called mine WPForo Members Only Spoiler).
Step 2: Add the Code
Paste in the following code:
function magicianleaks_restrict_spoiler_by_rating($content) { if ( is_admin() || !function_exists('WPF') ) { return $content; } // --- Configuration --- $required_rating = 6; // --------------------- $member_rating = 0; if ( is_user_logged_in() ) { $member = WPF()->member->get_member(get_current_user_id()); if ( isset($member['rating']['level']) ) { $member_rating = intval($member['rating']['level']); } } if ( $member_rating < $required_rating ) { $restriction_html = '<div class="wpf-spoiler-wrap"> <div class="wpf-spoiler-head wpf-spoiler-restricted">Spoiler (Restricted)</div> <div class="wpf-spoiler-body"> You must have a member rating of ' . esc_html($required_rating) . ' or higher to view this content. </div> </div>'; $pattern = '/<div class="wpf-spoiler-wrap">.*?<div class="wpf-spoiler-head">.*?<\/div>.*?<div class="wpf-spoiler-body">.*?<\/div>\s*<\/div>/s'; $content = preg_replace($pattern, $restriction_html, $content); } return $content; } add_filter('wpforo_content_after', 'magicianleaks_restrict_spoiler_by_rating', 99);
Step 3: Save & Test
-
Save and activate the snippet.
-
Clear your site cache.
That’s it! Spoilers will now only be visible to members who meet your required rating level (default is 6, but you can change it to whatever level you want).