0

For the shortcode I am using, the developer of the plugin only added the ability to output content via the shortcode. So the shortcode is all I have to work with.

Here is the default shortcode which does work:

echo do_shortcode("[theplugin_user_data user_id='8']");

With this code, it will output the data for the user with user id of "8". Problem is I can't use this code because if I put that it would give the same data for user id "8" to all comments, while I need it to fetch for each commenter.

So basically I need to fetch the user id of the current comment author, and after fetching their id I need to insert it dynamically into the shortcode.

Here is an idea of what I can come up with but I don't know how to make it work.

echo do_shortcode("[theplugin_user_data user_id='" . phpcodehere . "']");

So how can I get the current commenter's user id and insert it into the shortcode shown above?


Note:
I am putting this in the comments section to pull the data. I am using the default wordpress comments system, no plugins or external comments system (the plugin I am pulling data from doesn't have anything to do with comments).

Note 2:
The user ID that is needed is the commenter who MADE the comment, not who is logged in -- please note that this is inside a wordpress comments loop pulling all comments. Here is the comment loop:

function theme_comment( $comment, $args, $depth ) {
    $GLOBALS['comment'] = $comment;

For example, get_comment_author_link() works inside the loop to get the link for the comment author. What I need is the user ID of the comment author.

Note 3:
Not the post author, nor the logged in user; what I need is the comment author user ID.

1

So I'm assuming the "current commenter" is actually the current user logged into WP. So to get the current user ID, you can simply use get_current_user_id:

echo do_shortcode("[theplugin_user_data user_id='" . get_current_user_id() . "']");

UPDATE

Ok, so we want, then, the comment's author ID:

//$comment = get_comment(); // in your case, it seems you already have a $comment object...
if($comment) {
    if($comment->user_id) echo do_shortcode("[theplugin_user_data user_id='" . $comment->user_id . "']");
    else echo '(no data available)'; // <---- fallback, in case the comment has no known author
}

Hope this helps!

| improve this answer | |
  • Nope, current commenter is NOT the current user logged into wordpress. It was the user who made the comment! :) Please revise accordingly – user6686780 Feb 8 '17 at 20:50
  • Updated question with further details. – user6686780 Feb 8 '17 at 20:56
  • NOTE: I did try " . get_current_user_id() . " and that method does work for inserting php into the shortcode; now all I need is the correct call to get the comment author user ID, not the current logged in user. – user6686780 Feb 8 '17 at 21:03
  • Yep - just updated my answer – MacPrawn Feb 8 '17 at 21:03
  • Now I've got a problem. I noticed that it isn't fetching the correct user id every time. On some comments it is the correct id, but other comments it is the wrong id. How do I fix that? – user6686780 Feb 8 '17 at 21:08

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.