Inicio > Artículos, WordPress > WP-Polls + WordPress Multisite

WP-Polls + WordPress Multisite

1 septiembre, 2011

We all have seen or even used one of the most common plugins for WordPress: WP-Polls. This is an AJAX poll system to your WordPress blog. It works pretty nice because is extremely customizable via templates and css styles.

Problem

But it has just one big problem… does not work on a multisite installation.

When you press the “Vote” button, a message appears with an error like “poll ID missing” or something like that.

Why? Because of the AJAX implementation. Because you have a multisite installation, WordPress creates different tables for each site. It adds a prefix for each site tables. For example, the root site might have the default prefix “wp_”, so the next site would have “wp_1” and the next one “wp_2” and so on.

When this plugin makes an AJAX call, always looks for pools inside the default or root site database tables. In this example “wp_”, instead of looking at the tables of the current site.

Solution

It it very simple. We have to open the wp-polls.php file at the root of the plugin directory.

1. Find this function at the very top of the file.

[php] if (!function_exists(‘add_action’)) { [/php]

2. Put this code just right the end of the function.

[php] switch_to_blog($_REQUEST['blog_id']); [/php]

Your function, would look like this.

[php]
if (!function_exists(‘add_action’)) {
$wp_root = ‘../../..’;
if (file_exists($wp_root.’/wp-load.php’)) {
require_once($wp_root.’/wp-load.php’);
} else {
require_once($wp_root.’/wp-config.php’);
}
switch_to_blog($_REQUEST['blog_id']);
}
[/php]

3. Find these lines

[php]
wp_localize_script(‘wp-polls’, ‘pollsL10n’, array(
‘ajax_url’ => plugins_url(‘wp-polls/wp-polls.php’),
[/php]

4. Change the two lines for the following ones

[php]
//Cambio para enviar el ID del blog actual por AJAX
global $blog_id;
wp_localize_script(‘wp-polls’, ‘pollsL10n’, array(
‘ajax_url’ => get_bloginfo(‘url’).’/wp-content/plugins/wp-polls/wp-polls.php?blog_id=’.$blog_id,
[/php]

5. Save your changes.

Very simple but it works. You have to take on count that every time you update this plugin you have to make these changes again. Well, at least until they fix this.

Hope it works for you.

contacto

Mtro. Guillermo Humberto Vera Amaro