Skip to content Skip to sidebar Skip to footer

Wordpress - Get_comment_author() Returns Anonymous

I have developed a wordpress plugin that inserts a JS script in the head of every page of the site. I am trying to pass a couple variables to the script (in PHP) such as the name,

Solution 1:

As can be seen on this page, you need to supply an ID to that function, or it needs to be inside the loop. Because neither is true in your case, it returns anonymous.

Description Retrieve the author of the current comment. If the comment has an empty comment_author field, then 'Anonymous' person is assumed. This function is meant to live inside of the WordPress loop.

Usage

<?php$author = get_comment_author( $comment_ID ); ?>

To get comments by post, you can use the get_comments() function. You could use it as follows:

<?phpglobal$post; // get the current post$postId = $post->ID; // get the current post ID$comments = get_comments('post_id=' . $postId); // This gets all comments from current post?>

You can check the link on how you can use the output that follows.

If this is still unclear, let me know.

Post a Comment for "Wordpress - Get_comment_author() Returns Anonymous"