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, the email, and the website of the author of the comment, when there's a comment.
I have tried to use the get_comment_author(), get_comment_author_url() and get_comment_author_email() but it always returns "Anonymous", even if I just entered a name, website and mail adress when posting a comment.
Here is the code:
add_action('wp_head', 'insert_script');
function insert_script(){
$name = get_comment_author();
$website = get_comment_author_url();
$email = get_comment_author_email();
echo " <script type='text/javascript'>
var _gigo= _gigo || {};
_gigo['firstname'] = '' ;
_gigo['lastname'] = '".$name."' ;
_gigo['company'] = '".$website."' ;
_gigo['email'] = '".$email."' ;
</script>";
}
Do you know why the functions return an anonymous author and how I could fix it ? Thanks in advance.
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
.To get comments by post, you can use the get_comments() function. You could use it as follows:
You can check the link on how you can use the output that follows.
If this is still unclear, let me know.