
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.
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.
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.
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!






May 11, 2010 @ 22:48:28
May 12, 2010 @ 17:17:55
Hello:
Could not get this to activate. “Invalid – no header” error message.
May 12, 2010 @ 18:17:17
first, set your error_reporting to E_ALL to see the actual error.
what’s your WordPress and Magento version?
May 12, 2010 @ 17:27:42
my website wont display anything after the plugin is activated.
May 12, 2010 @ 18:17:43
set your error_reporting to E_ALL to see what’s your error.
May 25, 2010 @ 01:27:08
Great work Richard!
This is really getting interesting. I have been using Joomla with Magento for a couple of webshops but it feels like overkill most of the time. So a WordPress-Magento bridge plugin looks like a killer app!
I hope I will have time testing it during the summer…
May 25, 2010 @ 17:13:01
Thanks Kiper! Send a feedback of your testing either here or in the Mage Enabler page in the plugin repository in WordPress so I can improve it.
May 27, 2010 @ 03:35:18
Darn – went back and changed the code, but now there’s no statement showing up.
May 27, 2010 @ 09:24:19
Check the following:
May 28, 2010 @ 14:38:30
Looks like I have a final step of adding the Absolute URL:
Here’s the location of the Mage.php
/html/app/Mage.php
Wondering what I put into the Absolute URL field.
Aloha,
Greg
I’m hoping this will enable the Welcome Guest! comment to show.
May 28, 2010 @ 14:45:49
Put that
/html/app/Mage.phpaddress in the absolute URL textbox and Save the setting. It will display a message beside the box if it’s working properly.May 31, 2010 @ 15:01:50
I’ve transferred my blog to a new domain. For comments and inquiries regarding this article, please post it here.
How to use Magento’s session within WordPress
May 31, 2010 @ 15:54:34
May 06, 2012 @ 05:47:51