I am looking to change one line within a core function. The function is wp_allow_comment() located within /wp-includes/comment.php

function wp_allow_comment($commentdata) {
global $wpdb;
extract($commentdata, EXTR_SKIP);

// Simple duplicate check
// expected_slashed ($comment_post_ID, $comment_author, $comment_author_email, $comment_content)
$dupe = $wpdb->prepare( "SELECT comment_ID FROM $wpdb->comments WHERE comment_post_ID = %d AND comment_parent = %s AND comment_approved != 'trash' AND ( comment_author = %s ", wp_unslash( $comment_post_ID ), wp_unslash( $comment_parent ), wp_unslash( $comment_author ) );
if ( $comment_author_email )
    $dupe .= $wpdb->prepare( "OR comment_author_email = %s ", wp_unslash( $comment_author_email ) );
$dupe .= $wpdb->prepare( ") AND comment_content = %s LIMIT 1", wp_unslash( $comment_content ) );
if ( $wpdb->get_var($dupe) ) {
    /**
     * Fires immediately after a duplicate comment is detected.
     *
     * @since 3.0.0
     *
     * @param array $commentdata Comment data.
     */
    do_action( 'comment_duplicate_trigger', $commentdata );
    if ( defined('DOING_AJAX') )
        die( __('Duplicate comment detected; it looks as though you’ve already said that!') );

    wp_die( __('Duplicate comment detected; it looks as though you’ve already said that!') );
}

/**
 * Fires immediately before a comment is marked approved.
 *
 * Allows checking for comment flooding.
 *
 * @since 2.3.0
 *
 * @param string $comment_author_IP    Comment author's IP address.
 * @param string $comment_author_email Comment author's email.
 * @param string $comment_date_gmt     GMT date the comment was posted.
 */
do_action( 'check_comment_flood', $comment_author_IP, $comment_author_email, $comment_date_gmt );

if ( ! empty( $user_id ) ) {
    $user = get_userdata( $user_id );
    $post_author = $wpdb->get_var($wpdb->prepare("SELECT post_author FROM $wpdb->posts WHERE ID = %d LIMIT 1", $comment_post_ID));
}

if ( isset( $user ) && ( $user_id == $post_author || $user->has_cap( 'moderate_comments' ) ) ) {
    // The author and the admins get respect.
    $approved = 1;
} else {
    // Everyone else's comments will be checked.
    if ( check_comment($comment_author, $comment_author_email, $comment_author_url, $comment_content, $comment_author_IP, $comment_agent, $comment_type) )
        $approved = 1;
    else
        $approved = 0;
    if ( wp_blacklist_check($comment_author, $comment_author_email, $comment_author_url, $comment_content, $comment_author_IP, $comment_agent) )
        $approved = 'spam';
}

/**
 * Filter a comment's approval status before it is set.
 *
 * @since 2.1.0
 *
 * @param bool|string $approved    The approval status. Accepts 1, 0, or 'spam'.
 * @param array       $commentdata Comment data.
 */
$approved = apply_filters( 'pre_comment_approved', $approved, $commentdata );
return $approved;
}

I would like to change $approved = 'spam'; to $approved = 'trash'; - is this something that can be accomplished without hacking the core? I am trying to wrap my head around filters as a possible solution but not having any luck.

I have tried something like this:

add_filter('pre_comment_approved', 'custom_blacklist',1, 0);

function custom_blacklist() {
    if ( wp_blacklist_check($comment_author, $comment_author_email, $comment_author_url, $comment_content, $comment_author_IP, $comment_agent) )
        $approved = 'trash';
}

but it ended up killing spam filtering all together, I am definitely not using filters properly.

  • What is it that you're trying to achieve by changing that value? – hereswhatidid May 29 '14 at 21:52
  • @hereswhatidid Comments that contain blacklisted terms will end up in the trash instead of spam. – Josh Mountain May 29 '14 at 21:54
  • Why aren't you using the Akismet Plugin for WordPress. Half the stuff you will end up modifying in Core is already handled by this plugin – eyoung100 May 29 '14 at 22:08
  • @ECarterYoung as far as I know Akismet also moves everything to spam not trash. Maybe I would be better off filtering Akismet than the core though. – Josh Mountain May 29 '14 at 22:13
  • It moves everything into Spam, and then you move the entire spam to the Trash in chunks of 20. The only thing is, when the plugin updates, the update will overwrite your customization, and regardless of the approach AFAIK, you still have to empty the trash. I'll gladly move stuff myself in exchange for the automation the plugin provides, i.e I don't have to update the Black List – eyoung100 May 29 '14 at 22:21
up vote 2 down vote accepted

You aren't filtering correctly. Firstly, you aren't passing variables to the function, so your function has no way of knowing what $comment_author is, etc. If you had debug mode enabled you'd probably get errors about undefined variables. Secondly, you need to return a value.

Untested, but seems like it ought to work:

add_filter('pre_comment_approved', 'custom_blacklist', 10, 2 );

function custom_blacklist( $approved, $commentdata ) {
    extract($commentdata, EXTR_SKIP);
    if ( wp_blacklist_check($comment_author, $comment_author_email, $comment_author_url, $comment_content, $comment_author_IP, $comment_agent) )
        $approved = 'trash';

    return $approved;
}
  • If I am understanding this correctly, custom_blacklist() will run at the very end of wp_allow_comment(), so all the other normal comment filtering type stuff will still happen but it will run a second wp_blacklist_check() and overwrite 'spam' with 'trash' for those select comments? – Josh Mountain May 29 '14 at 22:21
  • Yes, that is correct. – helgatheviking May 29 '14 at 22:21

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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