Getting the error in my functions file that I cannot figure out how to fix. The error is:
Only variables should be passed by reference
for the line of:
$user_id = get_comment(get_comment_ID())->user_id;
The whole of the code is:
$user_id = get_comment(get_comment_ID())->user_id;
$user = new WP_User( $user_id );
$user_roles = $user->roles;
$user_role = array_shift($user_roles);
if ($user_role == 'administrator') {
echo '<div class="comment-tag admin-comment-tag">Admin</div><br>';
} elseif ($user_role == 'contributor') {
echo '<div class="comment-tag contributor-comment-tag">Contributor</div><br>';
} else {
echo ' ';
}
There is an answer for your question right in the Codex:
The first parameter of get_comment
function is:
$comment – (integer) (required)
The ID of the comment you’d like to fetch. You must pass a variable
containing an integer (e.g. $id). A literal integer (e.g. 7) will
cause a fatal error (Only variables can be passed for reference or
Cannot pass parameter 1 by reference).
Default: None
In your code, you’re passing a function result as this param, so it’s not a variable, so you get this error.
How to fix this? It’s pretty simple… Just use a variable in there:
$comment_id = get_comment_ID();
$user_id = get_comment( $comment_id )->user_id;
Or use a dummy variable (as shown in Codex):
$user_id = get_comment( $dummy = get_comment_ID() )->user_id;