0

I am trying to make the rating required for a comment but not required when it is a reply. Its not working, can you please help.

function custom_validate_comment() {
    //validate rating
    if( $comment->comment_parent==0) { 
    if( empty( $_POST['rating'])  )
            wp_die( __('Error: Please Include Your Rating (0 to 5 Stars)')    );
    }
}

add_action('pre_comment_on_post', 'custom_validate_comment');
0

$comment is not set inside that function. Look into variable scope.

$_POST['comment_parent'] should be set though.

function custom_validate_comment() {
    //validate rating
    if( isset($_POST['comment_parent']) && $_POST['comment_parent'] === 0) { 
    if( empty( $_POST['rating'])  )
            wp_die( __('Error: Please Include Your Rating (0 to 5 Stars)')    );
    }
}
add_action('pre_comment_on_post', 'custom_validate_comment');

You could also rewrite your function to take a parameter-- function custom_validate_comment($comment_id)-- and then use get_comment to grab information about that comment but since the parent is passed in the $_POST data (I checked), I don't see the point of that step.

  • If the answer solved the problem, please "Accept" it. – s_ha_dum Jun 20 '13 at 16:22

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.