0

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

1 Answer 1

0

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!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.