Well, the AI didn’t do THAT bad of a job. But either you or the AI didn’t deliver code for the functions get_users_who_commented
or get_number_of_comments_by_user
which are not wordpress functions but have to be coded seperatly.
Also, i would change the code a bit to use wordpress-internal functions like the comment query:
function my_custom_shortcode( $atts ) {
$atts = shortcode_atts( array(
'post_id' => get_the_ID(), // default value for post ID: active post
'role' => '', // default value for user role: none
), $atts );
if( ! $atts['post_id'] || ! ( 'post' == get_post_type($atts['post_id']) ) ){
//if the post id doesn't exist or is not a post, return nothing
return "";
}
//get a list of all users, if role is set, only of the role
$user_args = array();
if( ! empty( $atts['role'] ) ) {
$user_args['role'] = $atts['role'];
}
$users = get_users( $user_args );
$output = "";
if( $users ){
$output="<ul>";
foreach( $users as $user ){
// get the count of comments this user has made on this post
$args = array(
'type' => 'comment',
'post_id' => (int)$atts['post_id'],
'user_id' => $user->ID,
'number' => -1,
'count' => true
);
// The Comment Query
$number_of_comments = new WP_Comment_Query( $args );
if( $number_of_comments > 0){
$output.= '<li>Yes, User '.$user->display_name.' commented (' . $number_of_comments . ' comments)</li>';
} else {
$output.= '<li>Sorry, User '.$user->display_name.' did not comment.</li>';
}
}
$output.= '</ul>';
}
return $output;
}
add_shortcode( 'users_who_commented', 'my_custom_shortcode' );
Happy Coding!