Wednesday, January 19, 2011

WordPress Conflicts (and what I did to make them go away)

I, with the help of a co-worker, made an interesting discovery today while installing a plugin for a client at work.

The client wished to have custom WordPress registration emails sent to their users when they sign up to the site. I can't say as I blame them, as the drub emails they send off with just a username and password are boring as heck.

Anyway, while I had always found online that "Register Plus" was the best plugin to use for this in the world of WordPress, I was asked to use the "Welcome Email Editor", which was a very nice, simple looking plugin. I installed it on my sandbox blog, it worked exactly as one would like it to, so I pushed it to the live site, activated it and saw an ominous message...

Welcome Email Editor can not function because another plugin is conflicting. Please disable other plugins until this message disappears to fix the problem.

Well poop, I thought. I wanted to get this issue off my plate, as it seemed simple enough. However, I had a conflicting plugin.

The first thing I thought I could do was check out the Email Plugin to see why the error was being thrown. I searched the code and found the following, which was setting a boolean to false, thus displaying the conflict message...

if (!function_exists('wp_new_user_notification')) {
function wp_new_user_notification($user_id, $plaintext_pass = '') {
...
}
}
else {
$sb_we_active = false
}

I figured out which plugin was the cause of the conflict quite easily. I thought it was being caused by my call in that plugin to wp_new_user_notification, however, upon commenting it out, I realized that wasn't the problem.

I asked a colleague to help out, he was also a bit puzzled. After some searching on the web, we found this ticket in WordPress' trac system. It was then clear what the problem was...

As long as your plugin file has the function in the global scope when its included, that error shouldnt occur. I think theres 1 or 2 dodgy plugins which do a include_once('pluggables.php'); when they shouldnt.
That told us that it was likely that somewhere in the plugin, I was including "pluggable.php". I never remembered doing it (or even why I would have done it), and doing a search I found the following function...

if ( !function_exists('wp_get_current_user') ) {
function wp_get_current_user() {
// Insert pluggable.php before calling get_currentuserinfo()
require (ABSPATH . WPINC . '/pluggable.php');
global $current_user;
get_currentuserinfo();
return $current_user;
}
}

There was a good reason that I didn't remember doing it, because it wasn't me. I had been given this code by someone at the start of the project to "help" with some of the problems I was running into*. What happens, is that "pluggable" loads first, which will make it impossible for the Welcome Email Editor to overwrite the function it needs to in order to work.

With some quick changes to my plugin, I was able to remove that include, get everything working and learn a valuable lesson about not ever, EVER including "pluggable.php" in a plugin again.

* I realize it seems very cheap to blame someone else for a problem I have run into, but at the same time, I believe that everyone should be held accountable. It would be easy to blame all my problems on others, but I don't do that, only in cases where someone else is actually at fault.

Friday, January 14, 2011

Getting All Friends of a Facebook Application

Everyone loves Facebook.

Well almost everyone anyway...

Developer's are among those who've grown to hate Facebook and their constant API changes, although the new "Graph API" does appear to be making constant strides toward being a solid, stable environment for developers.

One of the problems I have run into when working with the API, is that I want my application to get all of a user's friends who also use the application. After scouring the (still incomplete) API docs, I turned to the forums, where I found the following function.


$facebook->api(array('method' => 'friends.getAppUsers'));


This, although currently undocumented, is a great feature for any application you build for Facebook where you want to see friend's scores, results, etc.

Hope it helps someone out there as it has helped me!

What it is!

This is a personal blog I am keeping to describe problems I have had in my programming life and the solutions I have found to said problems.

I am not out to answer anyone's questions but my own, but hopefully you may find some answers to questions you have.