Mage Enabler

Update (May 12, 2010): Mage Enabler is now available in the Official WordPress Plugin repository. Get it here!

It’s been a while since I posted my tweak that allows Mage object to be utilized within any WordPress installation. The tweak requires the blog owner to modify functions.php as stated in my post. I’m inspired by the feedback of the readers who used the tweak so I decided that a plugin that does the same thing should be made to prevent modifying the WordPress core files thus preventing the tweak to cause inconsistencies when an upgrade is necessary.

It is my pleasure to introduce to you, Mage Enabler plugin. Just copy the whole code snippet below and save it as mage-enabler.php. Place it inside a folder named mage-enabler together with the readme.txt available right after the code block. Please take time to read and follow the readme.txt instruction on how to install the plugin in your WordPress setup.

How to use the plugin?

To show you how the plugin works, I’ve decided to convert my example in my previous post by providing a single login page for both WordPress (subscriber) and Magento (customer). This setup assumes that the account credentials (subcriber/customer) in both database are the same and that the function collision problem between WordPress and Magento has been fixed. If you followed the functions.php update in that post, remove the block of code added at line 4123 to 4138.

Location of functions.php

path-to-your-root-htdocs/wordpress/wp-includes/functions.php

Let’s start by opening the index.php file of the WordPress theme Twenty Ten. I’m using version 3.0-beta1 of WordPress in this article. You can replicate the same code update to any WordPress version and theme you have.

wordpress_root\wp-content\themes\twentyten\index.php

Copy the necessary codes (lines 16-18 and 22-40) to make it similar below:

<?php

/**
 * The main template file
 *
 * This is the most generic template file in a WordPress theme
 * and one of the two required files for a theme (the other being style.css).
 * It is used to display a page when nothing more specific matches a query.
 * E.g., it puts together the home page when no home.php file exists.
 * Learn more: http://codex.wordpress.org/Template_Hierarchy
 *
 * @package WordPress
 * @subpackage Twenty Ten
 * @since 3.0.0
 */
 if(class_exists('Mage')){
 	Mage::getSingleton('core/session', array('name' => 'frontend'));
 }
?>

<?php get_header(); ?>
<!-- Magento's custom greeting -->
<div style="font-size: 15px; margin-bottom: 15px; border-bottom: 1px solid #000; padding-bottom: 10px;">
<?php
if(class_exists('Mage')){
	$session = Mage::getSingleton("customer/session");
	$magento_message = "Welcome ";
	// Generate a personalize greeting
	if($session->isLoggedIn()){
		$magento_message .= $session->getCustomer()->getData('firstname').' ';
		$magento_message .= $session->getCustomer()->getData('lastname').'!';
	}else{
		$magento_message .= "Guest!";
	}

	echo $magento_message;
}
?>
</div>
<!-- End of Magento's custom greeting -->

The purpose of the code change above is to display a ‘Welcome [customer name here]‘ when a customer is logged in, and show ‘Welcome Guest’ when they are not. Make sure that if you’re going to use any Magento related script in your code, always write it within the following if condition:

if(class_exists('Mage')){
	// Write your Magento codes here
}

This will prevent your page or theme files from breaking up if in case the plugin is deactivated, not working or if Mage.php can’t be found.

Open user.php file found at following address below:

path-to-your-root-htdocs/wordpress/wp-includes/user.php

Locate the function wp_authenticate_username_password() and find the similar code below. I found mine at line 108.

	if ( !wp_check_password($password, $userdata->user_pass, $userdata->ID) )
		return new WP_Error('incorrect_password', sprintf(__('<strong>ERROR</strong>: Incorrect password. <a href="%s" title="Password Lost and Found">Lost your password</a>?'), site_url('wp-login.php?action=lostpassword', 'login')));

Right after the code above, add the following code starting at line 111 to make it similar to the code below. This update allows us to run the login request of Magento within WordPress:

	if ( !wp_check_password($password, $userdata->user_pass, $userdata->ID) )
		return new WP_Error('incorrect_password', sprintf(__('<strong>ERROR</strong>: Incorrect password. <a href="%s" title="Password Lost and Found">Lost your password</a>?'), site_url('wp-login.php?action=lostpassword', 'login')));
	// Start Magento
	if(class_exists('Mage')){
		Mage::getSingleton('core/session', array('name' => 'frontend'));
		$session = Mage::getSingleton("customer/session");
		try{
			$login = $session->login($username, $password);
		}catch(Exception $e){
			// Do nothing
		}
	}

Finally, to call the Magento’s logout function, locate the file below and open it:

path-to-your-root-htdocs/wordpress/wp-login.php

Find the switch case statement at line 352. Add the necessary code to make it similar to the code below and save it:

switch ($action) {

case 'logout' :
	check_admin_referer('log-out');
	// Start Magento
	if(class_exists('Mage')){
		Mage::getSingleton('core/session', array('name' => 'frontend'));
		Mage::getSingleton("customer/session")->logout();
	}
	wp_logout();

Now test your WordPress by accessing the homepage at http://localhost/wordpress/. It should display the ‘Welcome Guest’ similar to the image below with a cookie named as ‘frontend’ in your Firebug cookie tab. That is your Magento cookie.

A Test WordPress homepage showing the default welcome message for Magento

A Test WordPress homepage showing the default welcome message for Magento

Clicking the login for WordPress redirects us to the login form like the one below while still showing the ‘frontend’ cookie. Since I have a customer with same credentials on both Magento (Customer) and WordPress (set as Subscriber), all I have to do is to use the username and password to login in the form below.

A Test WordPress login page with a generated Magento cookie

A Test WordPress login page with a generated Magento cookie

At this point, your Magento session should be running with an active customer session. To check if it does, go back to your homepage by clicking the top header text or going to http://localhost/wordpress/. It should display now the welcome message with your customer’s name.

A Test WordPress homepage showing the customer's name in the welcome message

A Test WordPress homepage showing the customer's name in the welcome message

For those who will use this plugin, let me know the version of WordPress and Magento you’re using so I can keep track of the list of versions in which this plugin is compatible. Until such time that the plugin is moved to the Official WordPress Plugin repository, I’ll accept and answer inquiries here thru comment/feedback form below. Mage Enabler is now available in the Official WordPress Plugin repository. Get it here!