Hacking the easy-fancybox WordPress plugin

Intro

The easy-fancybox WordPress is actually a rather simple but cool plugin to use, if it would work out-of-the-box in WordPress 3.x (on Debian unstable).

First of all, a bit of background information:

  • I’ve installed WordPress using apt-get install wordpress on a Debian Unstable server
  • I’ve created three WordPress sites, all three use the same DocumentRoot (/usr/share/wordpress), but they do have separate wp-content/ locations (each in their own home directory)
  • plugins are installed in one of the blogs
  • Symlinks are creating from the blog that installed plugin to /usr/share/wordpress/wp-content/plugins/

Now, I have never written a WordPress plugin before, nor have I been developing in PHP for years! But since I really liked this plugin so much, I decided to give it a go (PHP, how hard can it be … kids play with it!).

The Hack

After analyzing other WordPress plugins (copy-paste development, don’t we just all do it!), the hack to make it work was pretty straightforward:
 
The original code:

// ENQUEUE
wp_enqueue_script('jquery.fancybox', plugins_url($efb, __FILE__).'/jquery.fancybox.pack.js', array('jquery'), '1.3.1');
wp_enqueue_script('jquery.easing', plugins_url($efb, __FILE__).'/jquery.easing.pack.js', array('jquery'), '1.3');
wp_enqueue_script('jquery.mousewheel', plugins_url($efb, __FILE__).'/jquery.mousewheel.pack.js', array('jquery'), '3.0.2');
wp_enqueue_style('jquery.fancybox', plugins_url($efb, __FILE__).'/jquery.fancybox.css.php', false, '1.3.1');
add_action('wp_head', 'easy_fancybox');
}

My hack:

// ENQUEUE
wp_enqueue_script('jquery.fancybox', plugins_url($efb, basename(dirname(__FILE__))).'/easy-fancybox/jquery.fancybox.pack.js', array('jquery'), '1.3.1');
wp_enqueue_script('jquery.easing', plugins_url($efb, basename(dirname(__FILE__))).'/easy-fancybox/jquery.easing.pack.js', array('jquery'), '1.3');
wp_enqueue_script('jquery.mousewheel', plugins_url($efb, basename(dirname(__FILE__))).'/easy-fancybox/jquery.mousewheel.pack.js', array('jquery'), '3.0.2');
wp_enqueue_style('jquery.fancybox', plugins_url($efb, basename(dirname(__FILE__))).'/easy-fancybox/jquery.fancybox.css.php', false, '1.3.1');
add_action('wp_head', 'easy_fancybox');
}

And now the plugins works on my website and on all the other WordPress sites I run on my server (I’ve installed WordPress 3.x on my Debian server using the apt-get command.)

I’ve mailed the author my changes 😉

UPDATE: for 1.3.1.2

After talking back and forwards with the author of this great plugin, the final result of the hacking is the following:

// ENQUEUE
wp_enqueue_script('jquery.fancybox', plugins_url($efb, basename(dirname(__FILE__))."/fancybox.php").'/jquery.fancybox.pack.js', array('jquery'), '1.3.1');
wp_enqueue_script('jquery.easing', plugins_url($efb, basename(dirname(__FILE__)).'/fancybox.php').'/jquery.easing.pack.js', array('jquery'), '1.3.1');
wp_enqueue_script('jquery.mousewheel', plugins_url($efb, basename(dirname(__FILE__)).'/fancybox.php').'/jquery.mousewheel.pack.js', array('jquery'), '3.0.2');
wp_enqueue_style('jquery.fancybox', plugins_url($efb, basename(dirname(__FILE__)).'/fancybox.php').'/jquery.fancybox.css.php', false, '1.3.1');
add_action('wp_head', 'easy_fancybox');
}

UPDATE: 07/10/2010

This works for version 1.3.1.3. I’ve only changed the wp_enqueue_script() statements.

function easy_fancybox_enqueue() {
	// check if fancy.php is moved one dir up like in WPMU's /mu-plugins/
	// NOTE: don't use WP_PLUGIN_URL to avoid problems when installed in /mu-plugins/
	$efb_subdir = (file_exists(dirname(__FILE__).'/easy-fancybox')) ? 'easy-fancybox' : '';

	// ENQUEUE
	// register main fancybox script
	wp_enqueue_script('jquery.fancybox', plugins_url($efb, basename(dirname(__FILE__))."/fancybox.php").'/jquery.fancybox.pack.js', array('jquery'), '1.3.1');

	
	if( "none" != get_option("fancybox_transitionIn") || "none" != get_option("fancybox_transitionOut") ) {
		// first get rid of previously registered variants of jquery.easing (by other plugins)
		wp_deregister_script('jquery.easing');
		wp_deregister_script('jqueryeasing');
		wp_deregister_script('jquery-easing');
		wp_deregister_script('easing');
		// then register our version
		wp_enqueue_script('jquery.easing', plugins_url($efb, basename(dirname(__FILE__)).'/fancybox.php').'/jquery.easing.pack.js', array('jquery'), '1.3.1');

	}
	
	// first get rid of previously registered variants of jquery.mousewheel (by other plugins)
	wp_deregister_script('jquery.mousewheel');
	wp_deregister_script('jquerymousewheel');
	wp_deregister_script('jquery-mousewheel');
	wp_deregister_script('mousewheel');
	// then register our version
	wp_enqueue_script('jquery.mousewheel', plugins_url($efb, basename(dirname(__FILE__)).'/fancybox.php').'/jquery.mousewheel.pack.js', array('jquery'), '3.0.2');
	
	// register style
	wp_enqueue_style('jquery.fancybox', plugins_url($efb, basename(dirname(__FILE__)).'/fancybox.php').'/jquery.fancybox.css.php', false, '1.3.1');

}

UPDATE: 03/11/2010

This works for version 1.3.2. I’ve only changed the wp_enqueue_script() statements.

function easy_fancybox_enqueue() {
    // check if fancy.php is moved one dir up like in WPMU's /mu-plugins/
    // NOTE: don't use WP_PLUGIN_URL to avoid problems when installed in /mu-plugins/
    $efb_subdir = (file_exists(dirname(__FILE__).'/easy-fancybox')) ? 'easy-fancybox' : '';

    // ENQUEUE
    // register main fancybox script
    wp_enqueue_script('jquery.fancybox', plugins_url($efb, basename(dirname(__FILE__))."/fancybox.php").'/fancybox/jquery.fancybox-1.3.2.pack.js', array('jquery'), '1.3.2');

    if( "none" != get_option("fancybox_transitionIn") || "none" != get_option("fancybox_transitionOut") ) {
        // first get rid of previously registered variants of jquery.easing (by other plugins)
        wp_deregister_script('jquery.easing');
        wp_deregister_script('jqueryeasing');
        wp_deregister_script('jquery-easing');
        wp_deregister_script('easing');
        // then register our version
        wp_enqueue_script('jquery.easing', plugins_url($efb, basename(dirname(__FILE__)).'/fancybox.php').'/fancybox/jquery.easing-1.3.pack.js', array('jquery'), '1.3');

    }

    // first get rid of previously registered variants of jquery.mousewheel (by other plugins)
    wp_deregister_script('jquery.mousewheel');
    wp_deregister_script('jquerymousewheel');
    wp_deregister_script('jquery-mousewheel');
    wp_deregister_script('mousewheel');
    // then register our version
    wp_enqueue_script('jquery.mousewheel', plugins_url($efb, basename(dirname(__FILE__)).'/fancybox.php').'/fancybox/jquery.mousewheel-3.0.4.pack.js', array('jquery'), '3.0.4');

    // register style
    wp_enqueue_style('jquery.fancybox', plugins_url($efb, basename(dirname(__FILE__)).'/fancybox.php').'/fancybox/jquery.fancybox.css.php', false, '1.3.2');

}

UPDATE: 23/11/2010

This works for version 1.3.3:

function easy_fancybox_enqueue() {
    // check if fancy.php is moved one dir up like in WPMU's /mu-plugins/
    // NOTE: don't use WP_PLUGIN_URL to avoid problems when installed in /mu-plugins/
    $efb_subdir = (file_exists(dirname(__FILE__).'/easy-fancybox')) ? 'easy-fancybox' : '';

    // ENQUEUE
    // register main fancybox script
    wp_enqueue_script('jquery.fancybox', plugins_url($efb, basename(dirname(__FILE__))."/fancybox.php").'/fancybox/jquery.fancybox-1.3.3.pack.js', array('jquery'), '1.3.3');

    if( "none" != get_option("fancybox_transitionIn") || "none" != get_option("fancybox_transitionOut") ) {
        // first get rid of previously registered variants of jquery.easing (by other plugins)
        wp_deregister_script('jquery.easing');
        wp_deregister_script('jqueryeasing');
        wp_deregister_script('jquery-easing');
        wp_deregister_script('easing');
        // then register our version
        wp_enqueue_script('jquery.easing', plugins_url($efb, basename(dirname(__FILE__)).'/fancybox.php').'/fancybox/jquery.easing-1.3.pack.js', array('jquery'), '1.3');

    }

    // first get rid of previously registered variants of jquery.mousewheel (by other plugins)
    wp_deregister_script('jquery.mousewheel');
    wp_deregister_script('jquerymousewheel');
    wp_deregister_script('jquery-mousewheel');
    wp_deregister_script('mousewheel');
    // then register our version
    wp_enqueue_script('jquery.mousewheel', plugins_url($efb, basename(dirname(__FILE__)).'/fancybox.php').'/fancybox/jquery.mousewheel-3.0.4.pack.js', array('jquery'), '3.0.4');

    // register style
    wp_enqueue_style('jquery.fancybox', plugins_url($efb, basename(dirname(__FILE__)).'/fancybox.php').'/fancybox/jquery.fancybox.css.php', false, '1.3.3');

}

UPDATE: 08/03/2011

Patch for version 1.3.4.8

--- easy-fancybox.php_old       2011-03-08 10:38:07.000000000 +0100
+++ easy-fancybox.php   2011-03-08 10:41:35.000000000 +0100
@@ -226,7 +226,7 @@
 ';
 
 if ('1' == $overlaySpotlight)
-       echo '
+       echo '
 ';
 
 }
@@ -325,7 +325,7 @@
        $do_fancybox = false;
        $easing = false;
        foreach ($easy_fancybox_array as $value) {
-               // anything enabled?
+            // anything enabled?
                if ( '1' == get_option($value['options']['enable']['id'],$value['options']['enable']['default']) )
                        $do_fancybox = true;
                // easing anyone?
@@ -337,7 +337,7 @@
 
        // ENQUEUE
        // register main fancybox script
-       wp_enqueue_script('jquery.fancybox', plugins_url(FANCYBOX_SUBDIR.'/fancybox/jquery.fancybox-'.FANCYBOX_VERSION.'.pack.js', __FILE__), array('jquery'), FANCYBOX_VERSION);
+       wp_enqueue_script('jquery.fancybox', plugins_url('/easy-fancybox/fancybox/jquery.fancybox-'.FANCYBOX_VERSION.'.pack.js', basename(__FILE__)), array('jquery'), FANCYBOX_VERSION);
 
        foreach ($easy_fancybox_array as $value) {
                if( ( 'elastic' == get_option($value['options']['transitionIn']['id'],$value['options']['transitionIn']['default']) || 'elastic' == get_option($value['options']['transitionOut']['id'],$value['options']['transitionOut']['default']) ) && ( '' != get_option($value['options']['easingIn']['id'],$value['options']['easingIn']['default']) || '' != get_option($value['options']['easingOut']['id'],$value['options']['easingOut']['default']) ) ) {
@@ -352,7 +352,7 @@
                wp_deregister_script('jquery-easing');
                wp_deregister_script('easing');
                // then register our version
-               wp_enqueue_script('jquery.easing', plugins_url(FANCYBOX_SUBDIR.'/fancybox/jquery.easing-'.EASING_VERSION.'.pack.js', __FILE__), array('jquery'), EASING_VERSION);
+               wp_enqueue_script('jquery.easing', plugins_url('/easy-fancybox/fancybox/jquery.easing-'.EASING_VERSION.'.pack.js', basename(__FILE__)), array('jquery'), EASING_VERSION);
        }
 
        // first get rid of previously registered variants of jquery.mousewheel (by other plugins)
@@ -361,10 +361,10 @@
        wp_deregister_script('jquery-mousewheel');
        wp_deregister_script('mousewheel');
        // then register our version
-       wp_enqueue_script('jquery.mousewheel', plugins_url(FANCYBOX_SUBDIR.'/fancybox/jquery.mousewheel-'.MOUSEWHEEL_VERSION.'.pack.js', __FILE__), array('jquery'), MOUSEWHEEL_VERSION);
+       wp_enqueue_script('jquery.mousewheel', plugins_url('/easy-fancybox/fancybox/jquery.mousewheel-'.MOUSEWHEEL_VERSION.'.pack.js', basename(__FILE__)), array('jquery'), MOUSEWHEEL_VERSION);
 
        // register style
-       wp_enqueue_style('easy-fancybox.css', plugins_url(FANCYBOX_SUBDIR.'/easy-fancybox.css.php', __FILE__), false, FANCYBOX_VERSION, 'screen');
+       wp_enqueue_style('easy-fancybox.css', plugins_url('/easy-fancybox/easy-fancybox.css.php', basename(__FILE__)), false, FANCYBOX_VERSION, 'screen');
 
 }

In your WordPress plugins directory you will have to make the following symlinks:

$ cd _WORDPRESS_DIR_/wp-content/plugins/easy-fancybox/fancybox/
$ ln -s ../jquery.fancybox.css.php
$ ln -s ../easy-fancybox-nl.mo
$ ln -s ../easy-fancybox-nl_NL.mo
$ ln -s ../easy-fancybox.php
$ ln -s ../easy-fancybox.pota
$ ln -s .. fancybox

The plugin and the discussion can be found on http://4visions.nl/wordpress-plugins/easy-fancybox/

Comments are closed.