0

I have a WordPress comment form, with a custom field (added with the Advanced Custom Field plugin) where the visitor can chose from a options (these are page IDs). When submitting the form, I want this select value to replace the comment_post_ID that by default adds the current page ID.

What I'm trying to achieve is having a global comment form on just one page and then for the visitor to select a page (in our case, these are medical treatments) and then on submit, the comment is attached to the selected page, rather than the page the comment form is on.

I have tried the preprocess_comment filter, which works fine if I hardcode the ID:

function overwrite_comment_post_id( $commentdata ) {
    $commentdata['comment_post_ID'] = '1562';
    return $commentdata;
}
add_filter( 'preprocess_comment' , 'overwrite_comment_post_id' );

Since the preprocess_comment filter doesn't yet have a comment ID (comment hasn't been saved), I don't know how I can access my custom field value here?

I have also read about the comment_post action (as suggested here on StackExchange), but I can't figure out how to get my custom field here either.

If anyone has any other suggestions, I would be curious to hear them!

Updated code

function comment_post_id_update( $comment_ID, $comment_approved, $commentdata ) {
    $commentdata = [];
    $commentdata['comment_post_ID'] = $_POST['acf']['field_6278d64aecf19'];
    wp_update_comment($commentdata);
}
add_action( 'comment_post', 'comment_post_id_update', 10, 3 );
3
  • You can access your custom $_POST variables comment_post and it fires after the comment is saved in database. Within that function, you can call wp_update_comment to change the comment post id. But, in your case, you will have to assign a comment post id when the comment submitted, you can use a hidden field for that.
    – Abhik
    May 5, 2022 at 18:05
  • @Abhik Many thanks for the reply! Still struggling with this. After much trial and error, I managed to get the field data, but I can't figure out how to overwrite the comment post id. I have updated the code above with what I've tried now. It has a default value (the page ID where form is shown) that gets saved, but the wp_update_comment doesn't update comment_post_ID.
    – Karl
    May 9, 2022 at 10:36
  • Check my answer.
    – Abhik
    May 9, 2022 at 12:33

1 Answer 1

0

This bit of code will update the comment_post_ID with the value submitted using your custom form.

function wpse405448_update_comment( $comment_ID, $comment_approved, $commentdata ) {
    if ( isset($_POST['acf']['field_6278d64aecf19']) && !empty($_POST['acf']['field_6278d64aecf19']) ) {
        $comment_post_id = intval($_POST['acf']['field_6278d64aecf19']);
        
        $com_arr = array(
            'comment_ID' => $comment_ID,
            'comment_post_ID' => $comment_post_id,
        );
        
        wp_update_comment($com_arr);
    }
}
add_action( 'comment_post', 'wpse405448_update_comment', 10, 3 );

Make sure both the comment_ID and comment_post_ID are valid. Otherwise it wont get updated.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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