I added a custom field with comment form. Below is my code.First function return post id when using var_dump function.But the last function return NULL always.

class Post_Rating_Public_Helper {

    public static function prp_change_comment_form_defaults($commentdata, $comment_id = null ) {
        global $check;
        global $wpdb, $post;

        //// get rating value from database
        $ratingValues = $wpdb->get_results( "SELECT meta_value FROM ".$wpdb->prefix."commentmeta WHERE meta_key = 'rating'");
        $results = $wpdb->get_results( "SELECT option_value FROM ".$wpdb->prefix."options WHERE option_name = 'prp_settings'");

        var_dump($post->ID); // return post id

        $a = unserialize($results[0]->option_value);
        $b = $a['checkbox1'];
        $check = $a['checkbox1'];
        $stars_label = $a['rtlabel'];
        if($stars_label !== ''){
            $st_label = $stars_label;
            $st_label = 'Please rate';

        $arraytype= array();
        $post_type = get_post_type($post->ID);
            foreach($a as $key => $type){
                if($type === $post_type){
                    $arraytype[] = $type;


        if($b == 'yes'){
                if (in_array($post_type, $arraytype)) {
        echo '<fieldset class="rating">
        <legend>'.$st_label.'<span class="required">*</span></legend>
        <input type="radio" id="star5" name="rating" value="5" /><label for="star5" title="Rocks!">5 stars</label>
        <input type="radio" id="star4" name="rating" value="4" /><label for="star4" title="Pretty good">4 stars</label>
        <input type="radio" id="star3" name="rating" value="3" /><label for="star3" title="Meh">3 stars</label>
        <input type="radio" id="star2" name="rating" value="2" /><label for="star2" title="Kinda bad">2 stars</label>
        <input type="radio" id="star1" name="rating" value="1" /><label for="star1" title="Sucks big time">1 star</label>


    //////// save comment meta data ////////

    public static function prp_save_comment_meta_data( $comment_id ) {
        if ( ( isset( $_POST['rating'] ) ) && ( $_POST['rating'] != "") )
        $rating = wp_filter_nohtml_kses($_POST['rating']);
        add_comment_meta( $comment_id, 'rating', $rating );
    ///////// validate meta field /////////

    public static function prp_validate_comment_meta_data() {
        global $post;
        global $wpdb;
        $results = $wpdb->get_results( "SELECT option_value FROM ".$wpdb->prefix."options WHERE option_name = 'prp_settings'");
        $getPostID = $wpdb->get_results( "SELECT comment_post_ID FROM ".$wpdb->prefix."comments WHERE comment_post_ID = '".$post->ID."' ");
        $a = unserialize($results[0]->option_value); 
        $check = $a['checkbox1'];

        var_dump($post->ID); // return null

        if ( $a['checkbox1'] == 'yes' ) {

        $comment_post = get_post($commentdata['comment_post_ID']); //Get post object
        $post_type = $comment_post->post_type;
        $arraytype= array();

        foreach($a as $type){
            if($type === $post_type){
                    $arraytype[] = $type;

                //if (in_array($post_type, $arraytype)) {
                    if ( !isset( $_POST['rating'] ) || empty( $_POST['rating'] ) ){
                        if($_POST['comment_parent'] == 0){
                            wp_die( __( 'Error: Please rate this post.' ) );
        //return $commentdata;

} //end of class


$this->loader->add_action( 'comment_form_top', $plugin_public, 'prp_change_comment_form_defaults');
        $this->loader->add_action( 'pre_comment_on_post', $plugin_public, 'prp_validate_comment_meta_data', 9);
        $this->loader->add_action( 'comment_post', $plugin_public, 'prp_save_comment_meta_data' );

I didn't figure out what's the issue.


First function works fine, because comment form is displayed just after post. So it’s the same request and global variable post still contains current post.

On the other hand, comment_post is run in another request - a POST request containing the newly added comment. So there is no global post object, because no post should be displayed.

That’s why this function takes comment_ID as first param. This comment is already in DB, so you can select it and then get the post it’s assigned to.

If you want to use post in comment_post, you should do it based on its params:

add_action( 'comment_post', 'show_message_function', 10, 2 );
function show_message_function( $comment_ID, $comment_approved ) {
    $comment = get_comment( $comment_ID );
     $post_ID = $comment->comment_post_ID;

So... It has nothing to do with OOP. It’s about when and where is given function called and what global variables are available then.

  • Thanks. I send post id with hidden field from first function then get post id in third function using $_POST['id']; – Shoaib Saleem Jan 6 '19 at 8:26
  • Can you send some code example with your answer? – Shoaib Saleem Jan 6 '19 at 8:27
  • @ShoaibSaleem where do you do that? All I can see in your third function is you’re trying to use global post variable, then do a lot of strange queries, that make no sense, and so on... What code would you like to see posted? Your question is “why...”. I think I’ve already explained it... – Krzysiek Dróżdż Jan 6 '19 at 8:31
  • What is your way to get post id in third function? – Shoaib Saleem Jan 6 '19 at 8:42
  • @ShoaibSaleem It’s not my way - It’s the only correct way. Stop ignoring params of hook callbacks. – Krzysiek Dróżdż Jan 6 '19 at 8:44

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.