Simple shortcode to check if a user has commented on a certain post

You made 2 small errors.

First of all: Shortcode-Functions receive a maximum of 2 parameters: $attributes and $content.

$attributes is an array of all the attributes within the shortcode, like [shortcode post_id=123] the attribute post_id will end up in the $attributes array as $attributes['post_id'].

$content has the content between the opening and the closing shortcode tag, like in [shortcode]Content in between[/shortcode], the text Content in between would end up in the $content parameter.

So, if you want to give the post_id to your shortcode function, you need to get it from the first parameter.

The second error is that the function get_the_ID() doesn’t take any parameters, but returns the current post ID. So you don’t need to call get_the_ID( $post_id ), you just use the post_id you put in the shortcode attributes directly.

I put in an check if the attribute post_id is set. If not, it takes the current Post ID. If it is set, it looks for the comments for the attribute post_id.

Lastly, be sure to always return the result of shortcodes, never echo directly. Because of the point within the WordPress Code, at which shortcode functions are run, you could (and will) end up with errors or positions for your shortcode results that you not intended.

// function that runs when shortcode is called
function ccheck_shortcode( $attributes ) { 
   
    // Things that you want to do.
    $args = array(
        'user_id' => get_current_user_ID(), // use user_id
        'post_id' => get_the_ID()
    );
    if( isset( $attributes['post_id'] ) ){
         $args['post_id'] = absint($attributes['post_id']);
    } 

    $comment = get_comments($args);

    if(!empty($comment)){
      return 'You commented';
    }
    return "";
}
// register shortcode
add_shortcode('ccount', 'ccheck_shortcode');

Happy Coding!