AI Search
Classic Search
 Search Phrase:
 Search Type:
Advanced search options
 Search in Forums:
 Search in date period:

 Sort Search Results by:

Filter by custom fields

Topic prefix

AI Assistant
Notifications
Clear all

[Solved] Spoilers Only Show for Certain Member ratings.

2 Posts
2 Users
2 Reactions
3,647 Views
Posts: 1
Topic starter
Translate
English
Spanish
French
German
Italian
Portuguese
Russian
Chinese
Japanese
Korean
Arabic
Hindi
Dutch
Polish
Turkish
Vietnamese
Thai
Swedish
Danish
Finnish
Norwegian
Czech
Hungarian
Romanian
Greek
Hebrew
Indonesian
Malay
Ukrainian
Bulgarian
Croatian
Slovak
Slovenian
Serbian
Lithuanian
Latvian
Estonian
(@magicianleaks)
New Member
Joined: 7 months ago
[#63611]

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).

 

 2025 09 02 19 45 55

1 Reply
Posts: 102
Translate
English
Spanish
French
German
Italian
Portuguese
Russian
Chinese
Japanese
Korean
Arabic
Hindi
Dutch
Polish
Turkish
Vietnamese
Thai
Swedish
Danish
Finnish
Norwegian
Czech
Hungarian
Romanian
Greek
Hebrew
Indonesian
Malay
Ukrainian
Bulgarian
Croatian
Slovak
Slovenian
Serbian
Lithuanian
Latvian
Estonian
(@kezeo)
Estimable Member
Joined: 9 years ago

thanks


Reply
Share: