• This is driving me crazy, so any help is much appreciated.

    I use the following code to add a ‘twitter’ field to user profiles:

    <?php
    add_filter('user_contactmethods', 'my_user_contactmethods');
    
    function my_user_contactmethods($user_contactmethods){
    
      $user_contactmethods['twitter'] = 'Twitter Username';
    
      return $user_contactmethods;
    }
    
    ?>

    Then, I can use this function to display the post Author’s twitter username IF they have entered the field in their profile:

    <?php $username = get_the_author_meta('twitter');
    if ( $username ) { ?>
    <?php  } ?>

    Now I’m trying to get the twitter handle of the comment author to show in each comment. Using this (and other various attempts) did not work:

    <?php $username = get_comment_author_meta('twitter');
    if ( $username ) { ?>
    <?php  } ?>

    Any ideas? What variable do I need here?

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    Is get_comment_author_meta() a real function? Not familiar with that one. Seems to me you need to use get_user_meta(), which means you need a user ID, which can be had via get_comment(). There may be a more efficient way, but that’s the one I know off the top of my head.

    This will only work if the comment author is a registered user of course. This may always be the case on your blog, but it is not always the case on any blog.

    Thread Starter ICanHazCode

    (@icanhazcode)

    Can you put that together please?

    Moderator bcworkz

    (@bcworkz)

    I’m not sure of the context so I can’t give you a definite plug and play example. A common context would be to specify a callback in wp_list_comments(). The callback function is passed a comment data array, let’s use the conventional $comment to hold this array. Then somewhere in the callback where you want the twitter handle displayed, you’d just do something like this:

    $twitter = get_user_meta($comment->comment_ID, 'twitter', true);
    if ('' != $twitter) echo "Twitter: $twitter<br>";

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Get Comment Author Meta’ is closed to new replies.