The WP-FacebookConnect plugin for WordPress adds Facebook functionality to WordPress using the Facebook Connect APIs. Features include:
- Single-signon with your Facebook account
- Publish comments to the newsfeed
- Comment avatars display Facebook profile photos
I’ve noticed many flaws with this plugin in combination with the ‘smells like facebook‘ theme. Both the WP-FacebookConnect plugin as the smells-like-facebook theme are excellent pieces of software but it’s a pity that these pieces of software cooperate so little. Some of the issues I have with the combination:
- smells-like-facebook makes calls to the function get_avatar() in multiple places in such a way that WP-FacebookConnect does not understand it.
- WP_FacebookConnect will sometimes force you to log in even though you’re logged in already.
- WP_FacebookConnect will sometimes overwrite attributes for the user that have been modified after the user was already created.
- When not logged in, smells-like-facebook will allow users to enter name/email/website on trying to submit a comment on an article while it could simply include a ‘connect with facebook button’.
- WP_FacebookConnect does not implement get_gravatar()’s optional $size, $default or $alt parameters.
- WP_FacebookConnect does not implement a small login button
I will try to fix some of these issues in this article. I have imported both the smells-like-facebook theme and the WP-FacebookConnect plugin in my opensource Subversion repository so that the respective authors can look at my code changes and add this to their own code.
Using get_avatar()
From the function reference for get_avatar on the WordPress Codex we read:
- $id_or_email
- (int/string/object) (required) Author’s User ID (an integer or string), an E-mail Address (a string) or the comment object from the comment loop. Note: with most comment templates you can use
$commenthere, in order to display the gravatar of the commenter. In other templates within The Loop (for WordPress 2.7 and lower), you can useget_the_author_id()(deprecated in WordPress 2.8). For WordPress 2.8, please useget_the_author_meta('user_email').- Default: None
So what kind of argument should we pass to the get_avatar()’s $id_or_email parameter for WP-FacebookConnect to work properly? After all it may be an integer, a string or an object. The following code snippet from WP-FacebookConnect shows where things might go wrong:
function fbc_get_avatar($avatar, $id_or_email, $size, $default) {
global $dbg;
if (!is_object($id_or_email)) {
return $avatar;
}
What we clearly see here is that the function fbc_get_avatar(), will return $avatar unchanged if $id_or_email is not an object! The function fbc_get_avatar() is hooked into WordPress’ get_avatar function through a call to add_filter() :
add_filter('get_avatar', 'fbc_get_avatar', 10, 4);
Which according to the function reference for add_filter on the WordPress Codex means that the function fbc_get_avatar() gets hooked to the filter get_avatar with a priority of 10, accepting 4 parameters to the function call. I’m not familiar with any other functions being hooked to the get_avatar filter so I’m a bit curious where priority 10 comes from but it’s an optional parameter so let’s just make a mental note of this for now (as I would also like to enable other forms of logging in on the 3DN Technology Blog eventually, and each may come with their own specific get_avatar hook). If however we look at the smells-like-facebook (slf) theme template index.php for example: We see that slf makes calls to get_avatar() using get_the_author_email() inside The Loop. While there’s a beautiful page on the WordPress Codex called Plugin API/Filter Reference and some reference to the author’s email filters there is no description of get_the_author_email() anywhere on this page. There’s also no function reference for this function, so let’s dive into the guts of WordPress for a little bit here.
[fredl@omega:/web/prd/wordpress.3dn.nl/wp-includes$ grep get_the_author_email *
deprecated.php:function get_the_author_email() {
We see here that the function get_the_author_email() is defined in the standard include file deprecated.php which does not look promising. The function header says:
/**
* Retrieve the email of the author of the current post.
*
* @since 1.5
* @deprecated 2.8
* @uses $authordata The current author's DB object.
* @return string The author's username.
* @deprecated Use the_author_meta('email')
*/
function get_the_author_email() {
So slf should really not be using this function anymore but it gives us a good lead on the function we may need to use instead, which is get_the_author_meta(). Searching through the WordPress Codex does not return any function reference so we're back to diving into the guts of WordPress again:
/**
* Retrieve the requested data of the author of the current post.
* @link http://codex.wordpress.org/Template_Tags/the_author_meta
* @since 2.8.0
* @uses $authordata The current author's DB object (if $user_id not specified).
* @param string $field selects the field of the users record.
* @param int $user_id Optional. User ID.
* @return string The author's field from the current author's DB object.
*/
function get_the_author_meta($field = '', $user_id = false) {
What we see here is that get_the_author_meta() returns a string, which is not what we want our fbc_get_avatar() function to get as it's $id_or_email parameter. So neither get_the_author_email() nor get_the_author_meta() is really good enough here. For now I'll leave it at that, in a next article I will discuss some solutions to the above.
More Coding
If you think this was an interesting article you might be interested in reading more articles in the 3DN Techblog's Coding category:
- Playing with Simple Facebook Connect
- Facebook Bookmarks for Chrome?
- Add Ajax to Expertise Plugin
- Checking Comments for Requests
- Adding sendmail/SMTP to experts plugin