在不破坏核心的情况下更改WordPress核心函数

时间:2014-05-29 作者:Josh Mountain

我希望在核心功能中更改一行。功能是wp_allow_comment() 位于/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;
}
我想换衣服$approved = \'spam\';$approved = \'trash\'; - 这是不是可以在不破坏核心的情况下完成的?我正试图用过滤器作为可能的解决方案,但没有任何运气。

我试过这样的方法:

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\';
}
但它最终杀死了垃圾邮件过滤,我肯定没有正确使用过滤器。

1 个回复
最合适的回答,由SO网友:helgatheviking 整理而成

您没有正确过滤。首先,您没有将变量传递给函数,因此函数无法知道$comment_author 如果启用了调试模式,则可能会出现有关未定义变量的错误。其次,您需要返回一个值。

未经测试,但似乎应该有效:

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;
}

结束