I am writing a plugin that will moderate all posts regardless of what level user posted them. It will check the post content and title against a list of words and if any are in the post, I want to put the post in the moderation queue as if it were posted by a contributor. This part works fine, but I also want it to automatically delete posts with really bad words. This is how I currently have it:
function filter_handler( $data , $postarr ) {
// Get vars from data
$content = $data['post_content'];
$title = $data['post_title'];
// Array of words that will hold a post for aproval
$badWords = array(
"link=", "content-type", "bcc:", "cc:", "document.cookie",
"onclick", "onload", "javascript"
);
$reallyBadWords = array(
"query", "pluginTesting"
);
// If bad words exist in post, set post_status to pending
foreach ( $badWords as $word ) {
if (
strpos( strtolower( $content ), strtolower( $word ) ) ||
strpos( strtolower( $title ), strtolower( $word ) )
) {
$data['post_status'] = "pending";
}
}
// If really bad words exist, delete post
foreach ( $reallyBadWords as $word ) {
if (
strpos( strtolower( $content ), strtolower( $word ) ) ||
strpos( strtolower( $title ), strtolower( $word ) )
) {
$data['post_status'] = 'trash';
}
}
return $data;
}
add_filter( 'wp_insert_post_data', 'filter_handler', '99', 2 );
This works for the regular words but requires two really bad words to set a post to trash. Also using this method, when a user publishes a post with really bad words, they get directed to an error page telling them they are not allowed to edit posts in the trash.
What would be a good way to fix this so that it sends users back to the posts page and displays an error message informing them that their post was deleted?
Also, how can I fix the fact that it requires two really bad words before deletion?