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

Question

Hi all (mi first question on StackExchange, sorry in advance for my inability)
I want to make a shortcode to check if the current user has commented on a specific post.
I found this code as a starting point:

// function that runs when shortcode is called
function ccheck_shortcode( $post_id = false, $user_id = false ) { 
  
// Things that you want to do.
$args = array(
    'user_id' => get_current_user_ID(), // use user_id
    'post_id' => get_the_ID()
 );

$comment = get_comments($args);

if(!empty($comment)) echo 'You commented';

}
// register shortcode
add_shortcode('ccheck', 'ccheck_shortcode');

but i would like to specify the post id with a shortcode parameter, in order to use it everywhere,
( [ccheck postid=””] ) so i tried this:

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

$comment = get_comments($args);

if(!empty($comment)) echo 'You commented';

}
// register shortcode
add_shortcode('ccount', 'ccheck_shortcode');

Of course it doesn’t work. Tips?
Thank you in advance

0
Simone Bionda 2 months 2022-12-02T03:53:10-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse