• Hello,

    For privacy reasons I would like to change the comment Author’s IP address to 127.0.0.1 AFTER a moderator has approved it. If the comment is trashed or sent to “unwanted”, the IP should remain. The auto-approved comments are not concerned.

    I looked at the comments.php file in /wp-admin, made a few tries but it never works…

    If somebody can help… Thank you in advance!

Viewing 4 replies - 1 through 4 (of 4 total)
  • Dion

    (@diondesigns)

    You can use the preprocess_comment filter to change the IP address. Here’s a simple example that removes the IP address:

    function filter_comment_ip($data) {
    	$data['comment_author_IP'] = 'no_ip';
    	return $data;
    }
    
    add_filter('preprocess_comment', 'filter_comment_ip');
    Moderator bcworkz

    (@bcworkz)

    Dion is correct in a general sense, but specific to approving a pending comment, “preprocess_comment” does not fire, it’s for newly created comments. You should use “comment_unapproved_to_approved” action. Your callback is passed the current comment object, so you can check other criteria if need be before updating the comment with an alternative IP. If you call wp_update_comment() from this callback, you’ll start an infinite loop unless additional measures are taken. You can remove your callback from the action hooks first, or use $wpdb->update() to simply update the comment_author_IP field directly, bypassing the usual WP actions.

    Thread Starter ruzakuku

    (@ruzakuku)

    Thank you both, but where is this comment_unapproved_to_approved function?

    What I tried prior to open this thread: in comments.php after: (Line 2240 or so)

    $data = apply_filters( ‘wp_update_comment_data’, $data, $comment, $commentarr );

    $keys = array( ‘comment_post_ID’, ‘comment_content’, ‘comment_author’, ‘comment_author_email’, ‘comment_approved’, ‘comment_karma’, ‘comment_author_url’, ‘comment_date’, ‘comment_date_gmt’, ‘comment_type’, ‘comment_parent’, ‘user_id’, ‘comment_agent’, ‘comment_author_IP’ );

    I added here:

    if ($data[‘comment_approved’] = 1) {$data[‘comment_author_IP’]=”127.0.0.1″;}

    Then the script continues:

    $rval = $wpdb->update( $wpdb->comments, $data, compact( ‘comment_ID’ ) );
    $data = wp_array_slice_assoc( $data, $keys );
    etc

    It does not work… Why?!

    • This reply was modified 6 years, 5 months ago by ruzakuku.
    • This reply was modified 6 years, 5 months ago by ruzakuku.
    Moderator bcworkz

    (@bcworkz)

    Because wp_update_comment() is not called in the case of held comments being approved. The comment review form updates the comment status in the DB directly without updating other data. In any case, never alter core code to accomplish anything. Always go through action and filter hooks. “comment_unapproved_to_approved” is not a function, it’s an action that you hook, similar to how Dion hooked the filter ‘preprocess_comment’.

    Because it’s an action and not a filter, you need to update the IP field yourself. It’s best to do like the comment review form and directly update the field in the DB.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Change IP address when approving a comment’ is closed to new replies.