admin管理员组

文章数量:1317915

I'm trying to add a new key/value pair to my comment in the database by using a filter found in a WordPress plugin.

This is the function where the filter is defined:

/**
 * Parse comment before db insert
 *
 * @param WP_REST_Request $request Comment request.
 *
 * @return array
 */
public function prepare_comment_for_database( $request ) {

    $comment_fields = array(
        'comment_post_ID'        => sanitize_text_field( $request->get_param( 'post_id' ) ),
        'comment_approved'       => sanitize_text_field( $request->get_param( 'comment_approved' ) ),
        'comment_author'         => sanitize_text_field( $request->get_param( 'comment_author' ) ),
        'comment_author_email'   => sanitize_email( $request->get_param( 'comment_author_email' ) ),
        'comment_author_url'     => esc_url_raw( $request->get_param( 'comment_author_url' ) ),
        'comment_content'        => tvd_remove_script_tag( $request->get_param( 'comment_content' ) ),
        'comment_date'           => sanitize_text_field( $request->get_param( 'comment_date' ) ),
        'comment_karma'          => sanitize_text_field( $request->get_param( 'comment_karma' ) ),
        'comment_parent'         => sanitize_text_field( $request->get_param( 'comment_parent' ) ),
        'comment_type'           => sanitize_text_field( $request->get_param( 'comment_type' ) ),
        'user_id'                => get_current_user_id(),
        //Meta fields for comments
        'level'                  => wp_filter_kses( $request->get_param( 'level' ) ),
        'comment_author_picture' => esc_url_raw( $request->get_param( 'comment_author_picture' ) ),
        'upvote'                 => wp_filter_kses( $request->get_param( 'upvote' ) ),
        'downvote'               => wp_filter_kses( $request->get_param( 'downvote' ) ),
    );

    /* We sanitize by default the fields that we don't know */
    $extra_fields = $request->get_param( 'tcm_extra_fields' );
    if ( ! empty( $extra_fields ) ) {
        foreach ( $extra_fields as $key => $value ) {
            $extra_fields[ $key ] = sanitize_text_field( $value );
        }
        $request->set_param( 'tcm_extra_fields', $extra_fields );
    }

    /**
     * Possibility to add more fields before a comments is registered in the database
     *
     * @param array $comment_fields The fields from thrive comments
     * @param WP_REST_Request $request The comment request
     */
    return apply_filters( 'tcm_comments_fields', $comment_fields, $request );
}

But when I try and var_dump() using this code placed in my child theme's functions.php, the page just freezes and no var_dump() is shown:

function custom_add_comment_field( $comment_fields, $request ) {

    var_dump($comment_fields);
    var_dump($request);

    return $comment_fields;
}
add_filter( 'tcm_comments_fields', 'custom_add_comment_field', 10, 2 );

Why is my call back function not working?

I am trying to use var_dump() to see what the args contain on execution, but I also tried setting a new field as shown below. When doing this the page no longer freezes, but the field is also never added to the comment:

function custom_add_comment_field( $comment_fields, $request ) {

    $comment_fields['my_new_field_key'] = 'my new field value';

    return $comment_fields;
}
add_filter( 'tcm_comments_fields', 'custom_add_comment_field', 10, 2 );

本文标签: phpWhy does my callback function not work with this custom filter hook