0

Hello Im using a custom filter that hooks to pre_comment_approved to look for bad words in a custom way. Now when the comment gets flagged it returns spam.

How could I post back an error to the comments page so the user is aware that his/her email was sent to spam because of the use of a blacklisted word?

Thanks

function my_wp_blacklist_check( $approved , $commentdata ) {

$mod_keys = trim( get_option('blacklist_keys') );
if ( '' == $mod_keys )
    return false; // If moderation keys are empty
$words = explode("\n", $mod_keys );

foreach ( (array) $words as $word ) {
    $word = trim($word);
    str_pad($word, 1, STR_PAD_BOTH);
    // Skip empty lines
    if ( empty($word) ) { continue; }

    // Do some escaping magic so that '#' chars in the
    // spam words don't break things:
    $word = preg_quote($word, '#');

    $pattern = "#\b$word\b#i";
    if (preg_match($pattern, $commentdata['comment_content'])) {
        return 'spam';
    }
}
return true;
}
add_filter( 'pre_comment_approved' , 'my_wp_blacklist_check' , '99', 2 );
  • Post your code please. – s_ha_dum May 9 '13 at 20:53
1

This is how I've handled passing messages back to the user before: within your current function, set a transient with your message:

set_transient( 'admin_notice', 'Please put down your weapon. You have 20 seconds to comply.' );

Then add a new hooked function:

function admin_notices() {
    $notice = get_transient( 'admin_notice' );
    if ( $notice ) {
        echo '<div class="error"><p>' . $notice . '</p></div>';
        delete_transient( 'admin_notice' );
    }
}

add_action( 'admin_notices', 'admin_notices' );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.