Skip to:
Content
Pages
Categories
Search
Top
Bottom

Content warning if posts contain certain words


  • angeljs
    Participant

    @angeljs

    I was wondering it would be possible to add a word filter to bbPress? Then, if anyone posts a topic or post containing any of those words, a content notification box would pop up to warn anyone that the content may be offensive, and asking them to confirm whether or not they wish to continue to view the thread. That way, if admins want to allow free speech in their forums, they also have a way to warn visitors/members about any content they may not wish to view.

Viewing 8 replies - 1 through 8 (of 8 total)
  • Hi,
    Having them to be approved by a moderator is already possible with other tools, but the one you ask for I have never seen. The problem would probably be to have a database of ‘offensive’ terms, in all languages. Or would you leave that up to the admin to choose words?
    Pascal.


    angeljs
    Participant

    @angeljs

    It would be good if the admin could input their own words, as what some may deem offensive, others may not be bothered by.


    Ismail
    Participant

    @elhardoum

    Hello,

    I have made this a little simple, where any visitor can have an overlay division hiding the topic content and everything when there are caught words in the currently viewing topic..

    You can add this code to your theme’s functions file:

    Remember: edit $list = 'test,bad topic,'; line to insert your watched words separated by commas without a space after the commas.

    add_action('wp', function() {
    
    	/**
    	  * Add the watched words or sentences separated by commas.
     	  * Remember not to add a space before the words
     	  * Example : 'evil dog,naughty cat,pig'
    	  */
    
    	$list = 'test,bad topic,';
    	// set up a global variable for our case
    	$GLOBALS['my_filtered_words'] = explode(',', $list);
    
    });
    
    add_action('bbp_theme_after_reply_content', function() {
    
    	if( ! get_the_ID() )
    		return;
    
    	$topic = get_post( get_the_ID() );
    
    	if( ! $topic || 'topic' !== $topic->post_type )
    		return;
    
    	global $my_filtered_words;
    	$words = preg_split( "/\s+/", $topic->post_content );
    
    	foreach( $words as $i => $word )
    		$words[$i] = strtolower($word);
    
    	$occurance = 0;
    	foreach( $my_filtered_words as $string ) {
    		$string = strtolower( $string );
    		$occurance += in_array( $string, $words ) ? 1 : 0;
    	}
    
    	if( ! ( $occurance > 0 ) )
    		return; // nothing caught
    
    	$cookie = isset( $_COOKIE['se_conf_warned_topics'] ) ? explode(',', $_COOKIE['se_conf_warned_topics']) : array();
    
    	if( in_array($topic->ID, $cookie) )
    		return; // confirmed before.
    
    	?>
    
    		<style type="text/css">
    			.se-conf-wt {position: fixed; top: 0; left: 0; background: rgba(0, 0, 0, 0.78); width: 100%; height: 100%; z-index: 999;}
    			.se-conf-wt p {color: #fff; position: relative; top: 50%; display: table;margin: 0 auto;}
    			.se-conf-wt a {color:#fff;}
    		</style>
    		<div class="se-conf-wt t-<?php echo $topic->ID; ?>">
    			<p>This topic contains offensive content. <a href="javascript:;">Continue?</a></p>
    		</div>
    		<script type="text/javascript">
    			window.onload = function() {
    				var a = document.querySelector('.se-conf-wt.t-<?php echo $topic->ID; ?> a'),
    					b = document.body;
    				a.onclick = function() {
    				    var expires = new Date(),
    				    	cval = '<?php echo isset($_COOKIE['se_conf_warned_topics']) ? $_COOKIE['se_conf_warned_topics'] : ''; ?>';
    					expires.setMonth(expires.getMonth() + 6); // 6 months expiracy date
    					cval += '<?php echo $topic->ID; ?>,';
    					document.cookie = "se_conf_warned_topics=" + cval + ";  expires=" + expires;
    					// cookie set, now let's hide the warning
    					this.parentNode.parentNode.remove();
    					b.style.overflowY = '';
    					return false;
    				}
    				b.style.overflowY = 'hidden';
    			}
    		</script>
    
    	<?php
    
    });

    Tested on my local installation and it works fine.
    Let me know how it goes.

    Samuel

    @elhardoum , nice one !
    Thanks for sharing.


    angeljs
    Participant

    @angeljs

    Absolutely fantastic, thank you so much! Works perfectly! 🙂


    kieranw7261
    Participant

    @kieranw7261

    This is a great but I get an ‘Parse error: syntax error, unexpected T_FUNCTION’ error if I use this in:
    default/bbpress-functions.php

    Am I putting this in the correct place?


    Ismail
    Participant

    @elhardoum

    Thank you Pascal 🙂 !!
    angelis, you’re welcome. I’ll put this into a plugin and link you to the Github repository shortly.


    @kieranw7261
    my best guess is you’re running an EOL PHP version which is less than the recommended (see https://wordpress.org/about/requirements/)

    No worries just try this instead http://pastebin.com/XBeKjRFF and let me know if it worked.

    Samuel


    John James Jacoby
    Keymaster

    @johnjamesjacoby

    It’s actually much easier than this.

    If you set words, phrases, or IP addresses in your Settings > Discussions of WordPress’s admin, bbPress will adhere. Whatever you use for WordPress comments, also works for bbPress topics & replies.

    Hope that’s helpful!

Viewing 8 replies - 1 through 8 (of 8 total)
  • You must be logged in to reply to this topic.
Skip to toolbar