I added custom checkbox to the comment form. I am trying to write the post ID of this comment in the database. But without success. I'm trying with something like this:

add_action( 'comment_post', 'save_chbox_in_db' );

function save_chbox_in_db() {
     $post_id = ??? // Can't get id...
     insert_to_db ( $email, $post_id ); // custom function

I tested these options:

global $post;
$post_id = $post->ID();

global $wp_query;
$post_id = $wp_query->post->ID;

$post_id = get_queried_object_id();

$post_id = get_the_ID();

As a result, I get zero or nothing. Where is the problem? Something with "comment_post" action? Or do I make some stupid mistake?


comment_post hook pass to attached functions 3 parameters. The fourth parameter of add_action() indicates to how many parameters the attached function will be able to access.

add_action( 'comment_post', 'save_chbox_in_db', 10, 3 );

function save_chbox_in_db( $comment_ID, $comment_approved, $commentdata )
     $post_id = (int)$commentdata['comment_post_ID'];
     // other code
  • This is it! I was focused on a different path. Thank you very much! – Mark71 Jul 30 at 10:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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