• Resolved Protooler

    (@protooler)


    Hi,
    first I wanna thanks for this great plugin!!!
    I’m trying to customize something inside the comments.php
    for retrive the facebook avatar instead the default gravatar..

    I have create a query that stamp the avatar of the facebook user nearby the comments but when I try to post a comment the page stops
    and don’t communicate with wordpress database..
    the url in the browser is: https://www.mysite.com/index.php?fb_sig_in_iframe&wpbook=comment-handler

    Hereby the code

    <div id="commentlist">
    
        <?php $comment_class = 'acomment'; ?>
    
            <?php foreach ($comments as $comment) : ?>
    
            <div class="<?php echo $comment_class ?>">
    
            <?php
    
            $urlu = get_comment_author_url();
    
            if ($use_gravatar == "true" && !ereg("facebook",$urlu)){ 
    
            echo'<div class="gravatarr">';
    
                    $grav_url = "https://www.gravatar.com/avatar/" . 
    
                    md5(strtolower(get_comment_author_email())) . "?d=" . $gravatar_default . "&s=50&r=".$gravatar_rating;
    
          echo "<img src='$grav_url'/></div>";
    
           echo '<div class="gravatarpadding">';    
    
                                  }
    
            elseif ($use_gravatar == "true" && ereg("facebook",$urlu)){  
    
            echo'<div class="gravatarr">';
    
            $parsed = parse_url($urlu, PHP_URL_QUERY);
    
            $parsed = parse_str($parsed);
    
            $idfb = $parsed['id'];
    
            echo "<img src='https://graph.facebook.com/";
    
            echo "{$id}"; 
    
            echo "/picture?type=square' />";
    
            echo '</div><div class="gravatarpadding">';    
    
                                  }                        
    
        ?>
    
            <span class="wpbook_comment_author"><?php comment_author_link(); ?></span>
    
            <br/><span class="wpbook_comment_date"> <?php comment_date('F jS, Y') ?> at <?php comment_time() ?></span> 
    
            <span class="wpbook_comment_text">        
    
                    <?php if ($comment->comment_approved == '0') : ?>
    
                        <em>Your comment is awaiting moderation.</em>
    
                    <?php endif; ?>
    
                     <?php comment_text() ?></span>
    
                <?php if ($use_gravatar == "true"){ echo '</div>'; } ?>
    
            </div>
    
              <?php /* Changes every other comment to a different class */    
    
                        if ('acomment' == $comment_class){$comment_class = 'bcomment';} else {$comment_class = 'acomment';}
    
                    ?>
    
            <?php endforeach; /* end for each comment */ ?>
    
        </div><!-- //commentlist -->

    In my setting pages inside wordpress I have set a custom gravatar..

    If I delete $urlu = get_comment_author_url();
    in the code I’m able again to comment but obviously it doesn’t show me the facebook avatar..
    but it show only the custom gravatar..

    Any suggestion?

    Thank you
    Claudio

Viewing 15 replies - 1 through 15 (of 26 total)
  • Plugin Contributor B.

    (@bandonrandon)

    I don’t see where you are defining $id in your string to get the avatar. I see you set a $idfb I could be way off base but if $idfb is getting the correct user id you should be able to do something like.

    $idfb = $parsed['id'];
            echo "<img src='https://graph.facebook.com/".$idfb. "/picture?type=square' />";

    This is just a total guess but hope it is somewhat helpful.

    I also just looked up ereg and it looks like as of php 5.3 it’s depreciated and they want us to use preg_match instead so i may look into that as well.

    Thread Starter Protooler

    (@protooler)

    I retrive the id from the wordpress function get_comment_author_url();
    here: $urlu = get_comment_author_url();

    The idea is to show the gravatar if there is an email associated otherwise if there is the word “facebook” in the comment author url it stamp the facebook avatar..

    <?php
            $urlu = get_comment_author_url();
            if ($use_gravatar == "true" && !ereg("facebook",$urlu)){
            echo'<div class="gravatarr">';
                    $grav_url = "https://www.gravatar.com/avatar/" .
                    md5(strtolower(get_comment_author_email())) . "?d=" . $gravatar_default . "&s=50&r=".$gravatar_rating;
          echo "<img src='$grav_url'/></div>";
           echo '<div class="gravatarpadding">';
              }
            elseif ($use_gravatar == "true" && ereg("facebook",$urlu)){
            echo'<div class="gravatarr">';
            $urlu = get_comment_author_url();
            $parsed = parse_url($urlu, PHP_URL_QUERY);
            $parsed = parse_str($parsed);
            $idfb = $parsed['id'];
            echo "<img src='https://graph.facebook.com/";
            echo "{$id}";
            echo "/picture?type=square' />";
            echo '</div><div class="gravatarpadding">';
              }
        ?>

    I show all the avatar from facebook and from gravatar..so it work! ??

    The problem is when I try to post a comment in the form because
    there is something in conflict with the custom avatar..
    or with the query that retrive the variables in wpbook.php (i suppose)
    because the browser stops with a blank page and the url https://www.mysite.com/index.php?fb_sig_in_iframe&wpbook=comment-handler

    please help me/us to integrate this new feature..
    Thanx
    Claudio

    Plugin Author John Eckman

    (@johneckman)

    @claudio – you’ve added this new code to the comments.php in your primary theme, or the comments.php inside wpbook/theme/ ?

    When the user is inside Facebook, they aren’t seeing your normal theme, but the theme provided by WPBook – wpbook/theme/comments.php is used in that case.

    Can you start by validating that the WPBook code for submitting comments works without your changes, and then go from there?

    The comment-handler function is in wpbook.php lines 1068-1143, which gets invoked when the comments form is submitted inside Facebook and ends with a redirect – which clearly isn’t getting called in your case.

    Or are you talking about comments submitted outside Facebook, in your regular blog?

    Plugin Contributor B.

    (@bandonrandon)

    @john I think Claudio is using the right file I had the same problem with the comment-handler when I was echoing out extra content inside the loop. I belive I solved the problem now though.

    @claudio I took the time to rewrite your code. The main thing I did was to add the conditional before the $grav_url is outputed. Your original code also had a bug if the user entered “https://facebook.com/username&#8221; from the partent blog it would try to grab the id from that url which isn’t there.

    Here is what I came up with which is working in my test application. Add this to the top of theme/comments.php where the original $use_gravatar is replacing the code all to right before <span class="wpbook_comment_date">

    <?php if ($use_gravatar == "true"){
            $author_url = get_comment_author_url();
            if(preg_match("/facebook/i",$author_url)){
            $parse_author_url = (parse_url($author_url));
            $parse_author_url_q = $parse_author_url['query'];
                if(preg_match('/id[=]([0-9]*)/', $parse_author_url_q, $match)){
                    $fb_id = "/".$match[1];}
                else{ $fb_id = $parse_author_url['path'];
                }
            $grav_url= "https://graph.facebook.com".$fb_id."/picture?type=square";
            }
            else{
                $grav_url = "https://www.gravatar.com/avatar/" .
                    md5(strtolower(get_comment_author_email())) . "?d=" . $gravatar_default . "&s=50&r=".$gravatar_rating;
            }
            echo'<div class="gravatarr">';
          echo "<img src='$grav_url'/></div>";
          echo '<div class="gravatarpadding">';
         }

    Basiclly this checks to see if the user has “facebook” in there url if they do it then checks if they have “id” in the query sting. If they do it grabs that as the id, if not it grabs the path (“/username”) Then uses that as the avatar image.

    I also resized the gravatar to 50px instead of 60px just to match the size of the facebook avatars. Let me know if that does or dosen’t work for you you can see it in action at: https://apps.facebook.com/wpbooktestblog/blog/2010/09/02/another-test-post/

    If you want to download my comments.php or .diff file see https://bugs.wpbook.net/view.php?id=15
    Brooke

    Plugin Contributor B.

    (@bandonrandon)

    I found another bug in the above code. Change the first preg_match from
    if(preg_match("/facebook/i",$author_url)){
    to
    if(preg_match("@^(?:https://)?(?:www\.)?facebook@i",trim($author_url))){

    Basicly we want to ignore the domain if it has facebook in it but it’s not a profile or app page. Such as a subdomain (like apps.facebook or login.facebook)

    Thread Starter Protooler

    (@protooler)

    WOW this is great!!!
    Thanks BandonRandon, perfect work!
    I am happy to have given inspiration to this new feature!

    Plugin Contributor B.

    (@bandonrandon)

    Great, will you mark this as “resolved”?

    Thread Starter Protooler

    (@protooler)

    Sure! : )

    Is it possible to make the Facebook avatars show up along with the comments on the actual WordPress website, too?

    Plugin Contributor B.

    (@bandonrandon)

    If you want to use it on your WordPress site you should be able to modify the above to look something like this added to your themes functions.php:

    function get_fb_avatar($email,$url) {
    if(preg_match("@^(?:https://)?(?:www\.)?facebook@i",trim($url))){
            $parse_author_url = (parse_url($url));
            $parse_author_url_q = $parse_author_url['query'];
                if(preg_match('/id[=]([0-9]*)/', $parse_author_url_q, $match)){
                    $fb_id = "/".$match[1];}
                else{ $fb_id = $parse_author_url['path'];
                }
            $grav_url= "https://graph.facebook.com".$fb_id."/picture?type=square";
            }
            else{
                $grav_url = "https://www.gravatar.com/avatar/" .
                    md5(strtolower(email));
            }
            return $grav_url;
    
    }

    Then instead of get_avatar(); use get_fb_avatar(get_comment_author_email(),get_comment_author_url())

    This is untested but should work in thery. You also may have to pull out the get_comment_author_email() and get_comment_author_url() into variables and use them like $email=get_comment_author_email(); then call $email in the function.

    Let me know if this or something simular does the trick.

    Hm, I get this after adding the get_fb_avatar function to my functions.php file and calling for get_fb_avatar(get_comment_author_email(),get_comment_author_url()) in my theme:

    Parse error: syntax error, unexpected ';', expecting T_PAAMAYIM_NEKUDOTAYIM in /home/lxmbrg/public_html/mag/wp-content/themes/deadline/functions.php on line 409

    So, basically what I want is:

    Someone posts a new article on our wordpress website. It gets posted to my wall and our fan page wall (currently working). Then when someone comments on either the fan page wall or my wall the comment is “copied” to the wordpress blog, too (currently working). And that’s when I want the comment authors Facebook avatar to show up, and if not possible then our default gravatar should show up. Currently, our default gravatar is showing for any comments coming from Facebook.

    Or am I asking for too much here? I don’t know much about what limitations there are.. :/

    Plugin Contributor B.

    (@bandonrandon)

    Basicly my code was just untested and I had email instead of $email. The below function should work.

    function get_fb_avatar($email,$url) {
    if(preg_match("@^(?:https://)?(?:www\.)?facebook@i",trim($url))){
            $parse_author_url = (parse_url($url));
            $parse_author_url_q = $parse_author_url['query'];
                if(preg_match('/id[=]([0-9]*)/', $parse_author_url_q, $match)){
                    $fb_id = "/".$match[1];}
                else{ $fb_id = $parse_author_url['path'];
                }
            $grav_url= "https://graph.facebook.com".$fb_id."/picture?type=square";
            }
            else{
                $grav_url = "https://www.gravatar.com/avatar/" .
                    md5(strtolower($email));
            }
        $grav_img = "<img src='".$grav_url."'/>";
            return $grav_img;
    
    }

    Also when you add <?php echo get_fb_avatar(get_comment_author_email(),get_comment_author_url()); ?> make sure you have the closing simi colon. (I sometimes forget not everyone knows php and can fix these small typos)

    You can change the way the gravatar is outputed my changing the settings of the $grav_url = "https://www.gravatar.com/avatar/".md5(strtolower($email)); above to reflect your default and correct size.

    This function will also work on comments where the user put in there Facebook profile as there url.

    You’re a absolute genius.
    It’s working – and it looks good.

    Just one thing. How can I make it show our own default avatar whenever there’s no Facebook avatar to show?

    Which is this one:
    https://www.devotionmagazine.se/wp-content/themes/deadline/images/gravatar.png

    For now, it shows the standard, official Gravatar gravatar. (If you know which one I mean.)

    I know some PHP and edit a lot of code on a daily basis but a “hack” like this is a bit out of my range of knowledge. I really – really – appreciate your help!

    Don’t worry – I solved it.
    And also managed to get some css assignments in there, too.
    By doing this:

    function get_fb_avatar($email,$url) {
    if(preg_match("@^(?:https://)?(?:www\.)?facebook@i",trim($url))){
            $parse_author_url = (parse_url($url));
            $parse_author_url_q = $parse_author_url['query'];
                if(preg_match('/id[=]([0-9]*)/', $parse_author_url_q, $match)){
                    $fb_id = "/".$match[1];}
                else{ $fb_id = $parse_author_url['path'];
                }
            $grav_url= "https://graph.facebook.com".$fb_id."/picture?type=square";
            }
            else{
                $grav_url = "https://www.devotionmagazine.se/wp-content/themes/deadline/images/gravatar.png";
            }
        $grav_img = "<img src='".$grav_url." ' align='left' class='entry_author_image'/>";
            return $grav_img;
    
        }

    Like I said – thanks for your help. Been scratching my head a lot regarding this lately.

Viewing 15 replies - 1 through 15 (of 26 total)
  • The topic ‘[Plugin: WPBook] Comments Facebook avatar’ is closed to new replies.