Mage Enabler: A plugin to run Magento's session within WordPress

92 Comments

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!

92 Comments (+add yours?)

  1. Fred
    May 12, 2010 @ 17:17:55

    Hello:
    Could not get this to activate. “Invalid – no header” error message.

    Reply

    • Richard Feraro
      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?

      Reply

  2. louie
    May 12, 2010 @ 17:27:42

    my website wont display anything after the plugin is activated.

    Reply

    • Richard Feraro
      May 12, 2010 @ 18:17:43

      set your error_reporting to E_ALL to see what’s your error.

      Reply

  3. Kiper IT-konsult
    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…

    Reply

    • Richard Feraro
      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.

      Reply

  4. Greg Lutz
    May 27, 2010 @ 03:35:18

    Darn – went back and changed the code, but now there’s no statement showing up.

    Reply

    • Richard Feraro
      May 27, 2010 @ 09:24:19

      Check the following:

      1. Is Mage Enabler installed?
      2. Is it Activated?
      3. Did you supply the absolute URL of your Mage.php? Which of the following below is displayed when you supply the absolute URL?
        • Invalid URL
        • File is accessible!
        • Mage object not found!

      Reply

  5. Greg Lutz
    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.

    Reply

    • Richard Feraro
      May 28, 2010 @ 14:45:49

      Put that /html/app/Mage.php address in the absolute URL textbox and Save the setting. It will display a message beside the box if it’s working properly.

      Reply

  6. Camilo Sanchez
    Jun 07, 2010 @ 04:29:11

    Hi Richard, first thank you for this awesome plugin, this is just great work!
    I’ve installed the plugin and followed all the instructions as you said in this post but i have some trouble and i don’t know what is causing it. My problem is I’m getting the “Welcome Guest!” text but when i login into wordpress it remains the same. it doesn’t reflect my username, I can see the “frontend” cookie showing up. When I go to magento the same happens, it doesn’t show my username. What am i doing wrong? Another question i have, since is a little bit confusing for me is do i need to be registered in magento and wordpress with the same user and same password in both platforms? sorry if this last question is silly, I have little programming skills.
    I’m using wordpress 3.0 and magento 1.4. Another thing I’m seeing is two frontend cookies, one for magento and another for wordpress. Thank you in advance for your help!

    Reply

    • Richard Feraro
      Jun 07, 2010 @ 11:35:10

      Hi Camilo :)

      Thanks for using the plugin and reading my blog. Regarding your question, if you follow the modification in index.php, user.php and wp-login.php, manually create the same username and password in Magento and WordPress. Just go through the usual way registering to each application then test it again. That’s what I did to test if it works :)

      Thanks again.

      Richard

      Reply

  7. Gregg
    Jun 08, 2010 @ 03:16:27

    first, great blog! second, Im having an issue getting this to work. I have the plugin installed and it points to the absolute url for Mage.php at /var/www/vhosts/….etc

    I have all php errors turned on at .htaccess level in WP, but im still only seeing a blank white page.

    any help would be great! im using latest WP and Mage.

    What I am trying to do get the head, header and footer from Magento to be displayed on the WP install.

    thanks in advance!

    Reply

    • Richard Feraro
      Jun 08, 2010 @ 18:54:28

      Hello Gregg,

      Try placing the error_reporting(E_ALL) within the file your working on just above the script where you added the Magento call. It happened to me one time when I tried to declare the error_reporting in .htaccess but still got a blank screen. Let me know the output. Blank screen means there’s a Fatal error within the file.

      Thanks.

      Reply

      • Gregg
        Jun 10, 2010 @ 01:32:57

        thanks Richard,

        i realized I needed to get rid of Magento’s __() function so I did add the /app/code//local/Mage/Core/functions.php tweak you suggested.

        now my error is related to another plugin (wordpress breadcrumbs), so thanks for the help! you got me in the right direction.

        Reply

        • Richard Feraro
          Jun 10, 2010 @ 02:26:51

          Hi Greg,

          If you’re already using the Mage Enabler plugin, you don’t have to edit and add the tweak to functions.php anymore since the plugin already does that for you. You still need to get rid of the Magento’s __() translation function though.

          Ooops! My bad :) I thought you we’re referring to WordPress’ functions.php. Glad that you were able to solve it :)

          Thanks :)

  8. mik
    Jun 14, 2010 @ 19:30:52

    Hallo Richard,
    very useful plugin and very well supported tru your blog.
    I’ve successfully get rid of magento ___() function, installed mage-enabler and get the right absolute URL for for mage.php.
    Now, I’m not very interested in the double log in but I just want the magento store running inside my wp! I’m a little puzzled about where to place the call to magento. I’m thinking about create a wp-page called store, create a template for it and inserting the call to magento stuff in there (as described in the various comment to this and to the other post). Is it correct?

    Thank you for doing this!

    Bye

    Reply

    • Richard Feraro
      Jun 14, 2010 @ 19:59:41

      Hello Mik! Thanks for spending time reading my article.

      Yes, you’re right regarding the technique you mentioned. The best places I think to add the Magento call are the following files within your template folder: index.php, header.php or try functions.php if you’re template has one. Just make sure you’re Magento call is on top of the page where there’s no header request or you’ll encounter the error I mentioned in the post. You can test which one is the right file for your template by checking if you’re blog creates a frontend cookie when accessed thru a browser.

      Reply

  9. mik
    Jun 14, 2010 @ 23:43:06

    Hallo Richard, well spent time reding your blog ;)

    Back to php stuff, I’ve put the php call ( * Run Magento’s Mage.php within WordPress
    etc…) inside my wp-content/theme/mytheme/functions.php
    Than, I’ve created a template to embed the magento code in my wp’s “store” page
    and placed the function BEFORE the header call, like this:

    <?php
    /*
    Template Name: store
    */
    magento();
    ?>
    <?php get_header(); ?>
    

    So I’ve put some simple magento code in the page wrapped in if function

    <?php
    if(class_exists('Mage')){
    	// Write your Magento codes here
    
    require_once 'app/Mage.php';
    umask(0);
    //not Mage::run();
    Mage::app('default');
    
    //code snipped
    $className = Mage::getConfig()
                ->getBlockClassName('catalog/product_events');
    $block = new $className();
    
    $block->setTemplate('catalog/product/homepageevents.phtml');
    
    echo $block->renderView();
    }?>
    

    but I still get the infamous Fatal error: Uncaught exception ‘Exception’ with message ‘Warning: session_start() etc etc (20 lines…)

    Where did I get wrong?

    Thanks a lot

    Reply

    • Richard Feraro
      Jun 15, 2010 @ 02:16:03

      If you have installed and activated the Mage Enabler plugin and supplied the absolute URL with no error after saving it, you don’t have to use magento() anymore. You also don’t need the following lines because the plugin already does this for you:

      ...
      require_once 'app/Mage.php';
      umask(0);
      //not Mage::run();
      Mage::app('default');
      ...

      You just have to use the following code at the top of your page where you want to call Magento’s object (this is the one that creates the frontend cookie):

      // Start Magento
      if(class_exists('Mage')){
          Mage::getSingleton('core/session', array('name' => 'frontend'));
      }
      

      in the same file but in a different location depending on what you’re trying to do, add your own Magento calls:

      if(class_exists('Mage')){
          //code snipped
          $className = Mage::getConfig()->getBlockClassName('catalog/product_events');
          $block = new $className();
          $block->setTemplate('catalog/product/homepageevents.phtml');
          echo $block->renderView();
      }

      Reply

  10. mik
    Jun 16, 2010 @ 23:32:40

    Hi Richard,
    I’ve tryied going the easy way as you described: no more fatal error and the frontend cookie appears.

    I’ve my ‘store.php’ page with //start magento on top (before header!) and the code snipped you’ve suggested somewhere in the page where’d like to dislplay the magento’s stuff. But I get this error loading the page:

    Fatal error: Class ‘Mage_Catalog_Block_Product_Events’ not found in /var/www/myhost/wordpress/wp-content/themes/mytheme/store.php on line 46

    here comes the pages’s code:

    <?php
    /*
    Template Name: store
    */
    // Start Magento
    if(class_exists('Mage')){
        Mage::getSingleton('core/session', array('name' => 'frontend'));
    }
    
    ?>
    
    <?php get_header(); ?>
    <div class="clear"></div>
    <div id="catmenucontainer">
    	<div id="catmenu">
    <!--
    			<ul>
    				<?php wp_list_categories('sort_column=name&title_li=&depth=4'); ?>
    			</ul>
    -->
    	</div>
    </div>
    <div class="clear"></div>
    <div id="content_store">
    
    <!-- inserting magento's ciccia -->
    <?php
    if(class_exists('Mage')){
        //code snipped
        $className = Mage::getConfig()->getBlockClassName('catalog/product_events');
        $block = new $className();
        $block->setTemplate('catalog/product/homepageevents.phtml');
        echo $block->renderView();
    }?> 
    
    <!-- end ciccia -->
    
    	<div id="navigation">
    	<?php if(function_exists('wp_pagenavi')) { wp_pagenavi(); } ?>
    	</div>
    
    </div>
    
    <?php get_footer(); ?>
    

    I can feel I’m close to the solution…

    Thank you for your commitment!

    bye

    mik

    Reply

  11. mik
    Jun 16, 2010 @ 23:34:46

    ehm I realize there’s some italian language left in the comments, btw ‘ciccia’ stands for ‘stuff’ ;)

    Reply

  12. mik
    Jun 16, 2010 @ 23:49:02

    My bad, I see I were trying to add stuff I didn’t have on my magento such as ‘catalog/product_events’ or ‘catalog/product/homepageevents.phtml’

    I’ve tried inserting instead the code for the cart and … uuuuu … it works!
    Now I’ve understand how to handle this magento code…

    Thanks a lot for your help!

    mik

    Reply

    • Richard Feraro
      Jun 17, 2010 @ 01:49:15

      Great! I’m glad that you were able to figure it out on your own. :) Keep on reading mik!

      Reply

    • Richard Feraro
      Jul 03, 2010 @ 00:13:30

      Check out my latest post regarding this one.

      Reply

  13. Michael
    Jul 01, 2010 @ 00:04:11

    Nice plugin but just dosen’t seem to work for me… I have tried both theories (the plugin & manually adding the script) i get all the signs of it working i.e the frontend coming through the wp-login HOWEVER…. I can’t seem to get it login in via my wordpress and make it login through magento,
    I m using WP 3 magento 1.4, what i have noticed is that when you login to wordpress you are asked for your USERNAME and PASSWORD, when you login to Magento it asks you for EMAIL ADDRESS AND PASSWORD.. I am assuming this is why you can login via wordpress to both WP & Magento?

    Cheers
    Michael

    Reply

  14. mik
    Jul 04, 2010 @ 16:58:00

    Hallo Richard, here I come again!

    I’ve a new problem since I’m using the xml-rpc API [http://codex.wordpress.org/XML-RPC_wp] it works like a charm until I activate the mage-enabler plugin. When I turno on your plugoin the xmlrpc.php goes nuts and report this error:

    Fatal error: Uncaught exception 'Exception' with message 'Warning: Varien_Autoload::include(WP/User/Search.php) [<a href='varien-autoload.include'>varien-autoload.include</a>]: failed to open stream: No such file or directory in /var/www/mysite/magento/lib/Varien/Autoload.php on line 93' in /var/www/mysite/magento/app/code/local/Mage/Core/functions.php:248 Stack trace: #0 /var/www/mysite/magento/lib/Varien/Autoload.php(93): mageCoreErrorHandler(2, 'Varien_Autoload...', '/var/www/mysite...', 93, Array) #1 /var/www/mysite/magento/lib/Varien/Autoload.php(93): Varien_Autoload::autoload() #2 [internal function]: Varien_Autoload->autoload('WP_User_Search') #3 [internal function]: spl_autoload_call('WP_User_Search') #4 /var/www/mysite/wordpress/wp-admin/includes/user.php(472): class_exists('WP_User_Search') #5 /var/www/mysite/wordpress/wp-admin/includes/admin.php(46): require_once('/var/www/mysite...') #6 /var/www/mysite/wordpress/xmlrpc.php(54): include_once('/var/www/mysite...') #7 {main} thrown in /var/www/mysite/magento/app/code/local/Mage/Core/functions.php on line 248
    

    Any idea?

    thx

    bye

    mik

    Reply

    • Richard Feraro
      Jul 04, 2010 @ 19:50:52

      The xmlrpc setting is found in an admin page. Are you doing any Magento calls within the admin? There’s nothing in the error that says Mage Enabler is the culprit. The xmlrpc in my blog is actually enabled too with Mage Enabler installed as well.

      Reply

  15. mik
    Jul 04, 2010 @ 22:50:44

    uhm, actually it shouldn’t be any connection bettween mage enabler and the xmlrpc. I’m using it just to get an xml list of wp pages to create a flash menu. but without even linking magento or any wp pages with magento the file at http://mydomain.com/xmlrpc.php stops working. I was just wandering how it would be that xmlrpc get an error related to a magento’s function!

    Reply

    • Richard Feraro
      Jul 06, 2010 @ 22:41:05

      Hey mik, found out that this is a bug. I already updated the plugin. Please download the latest version.

      See the Changelog. Thanks for the feedback.

      Reply

  16. mik
    Jul 08, 2010 @ 19:42:55

    Hallo Richard, glad to be helpful. I’ve installed the latest mage-enabler version and now the xmlrpc goes like a charme. Unfortunately now I get a similar conflict/error with the popular plugin swf-object:

    Fatal error: Uncaught exception 'Exception' with message 'Warning: Varien_Autoload::include(Swfobject.php) [<a href='varien-autoload.include'>varien-autoload.include</a>]: failed to open stream: No such file or directory in /var/www/toystoys/magento/lib/Varien/Autoload.php on line 93' in /var/www/toystoys/magento/app/code/local/Mage/Core/functions.php:248 Stack trace: #0 /var/www/toystoys/magento/lib/Varien/Autoload.php(93): mageCoreErrorHandler(2, 'Varien_Autoload...', '/var/www/toysto...', 93, Array) #1 /var/www/toystoys/magento/lib/Varien/Autoload.php(93): Varien_Autoload::autoload() #2 [internal function]: Varien_Autoload->autoload('swfobject') #3 [internal function]: spl_autoload_call('swfobject') #4 /var/www/toystoys/wordpress/wp-content/plugins/nextgen-gallery/lib/swfobject.php(2): class_exists('swfobject') #5 /var/www/toystoys/wordpress/wp-content/plugins/nextgen-gallery/widgets/widgets.php(53): require_once('/var/www/toysto...') #6 /var/www/toystoys/wordpress/wp-content/plugins/nextgen-gallery/widgets/widgets.ph in /var/www/toystoys/magento/app/code/local/Mage/Core/functions.php  on line 248

    My silly solution is just to comment out line 248 in var/www/mysite/magento/app/code/local/Mage/Core/functions.php

    throw new Exception($errorMessage);

    Well, know the error doesn’t show in wp anymore and swf-object plugin is back to work as usual. Even Magento seems running quite well, anyhow I suppose there’s something wrong.

    Your thoughts?

    Thank you

    -mik

    Reply

    • Richard Feraro
      Jul 08, 2010 @ 20:06:34

      What’s the name of the plugin? Is it the Nextgen gallery plugin?

      Reply

  17. mik
    Jul 08, 2010 @ 20:27:06

    You get it! Even if the plugin reported in the error is swfobject http://wordpress.org/extend/plugins/wp-swfobject the error appears on an instance of Nextgen gallery’s slideshow (using imagerotator.swf). It’s a known bug?

    Reply

    • Richard Feraro
      Jul 08, 2010 @ 22:46:48

      I’m not sure yet unless I could see how that plugin works. Any sample site where I can see the error? How about additional details such as which file is showing an error? Does this error occurred prior to your xmlrpc.php inquiry? How can I replicate the error on your end?

      Reply

  18. cody
    Jul 09, 2010 @ 16:20:53

    i can’t get the valid Mage.php address.
    where can i find it?

    Reply

    • Richard Feraro
      Jul 09, 2010 @ 16:32:48

      Are you using windows or linux? Mage.php is found in /your-web-root/magento-root-folder/app/Mage.php

      Reply

      • cody
        Jul 10, 2010 @ 09:33:13

        im using windows, n my hosting is linux. i can only found /wp-content/plugins/mage-enabler. but cant find any other magento folder. is it i miss any step for installing?

        Reply

        • Richard Feraro
          Jul 10, 2010 @ 13:47:47

          I see. You don’t have Magento installed. You have to install it. Download it here.

  19. Mark
    Jul 12, 2010 @ 20:09:36

    I am trying to get the magento header to display in wordpares using

    if(class_exists('Mage')){
    $className = Mage::getConfig()->getBlockClassname('page/html_header');
    $block = new $className();
    $block->setTemplate('page/html/header.phtml');
    echo $block->renderView();
    

    The logo and slogan images display but its missing links, cart, search and menu.

    Any ideas?

    Reply

    • Richard Feraro
      Jul 12, 2010 @ 20:36:35

      Hi Mark,

      Thanks for reading the article. If you check my example above, it doesn’t have the links, cart info and other information you’re looking for because it is the header block only. The HTML blocks you’re looking for are found in the other blocks such as ‘catalogsearch/form.mini.phtml’ and ‘catalog/navigation/top.phtml’.

      Check the comments of peaker from the other article on how he did it here.

      Reply

    • Richard Feraro
      Jul 12, 2010 @ 20:44:54

      I also posted a script which pulls the rest of the links and search box too here.

      Reply

  20. Mark
    Jul 13, 2010 @ 05:37:23

    Perhaps I am a little slow today. I took a look at the other comments but I must be missing something.

    How would I do the following for topLinks?

    getChildHtml('topLinks')
    
    	$topLinks = $magento_block->createBlock('???/???');
    	$topLinks->setTemplate('???/???');
    
    <?php if(class_exists('Mage')){ echo  $topLinks->toHTML(); } ?>

    Thanks.

    Reply

  21. Mark
    Jul 14, 2010 @ 05:23:38

    I get this error.

    Fatal error: Call to undefined function isloggedin() in /public_html/blog/wp-content/themes/twentyten/index.php on line 12

    Here is my code.

    <?php
    // Start Magento
    if(class_exists('Mage')){
        Mage::getSingleton('core/session', array('name' => 'frontend'));
    }
    
    ?>
    
    <?php get_header(); ?>
    
    <?php
    if(isLoggedIn()){
    		$magento_message .= $session->getCustomer()->getData('firstname').' ';
    		$magento_message .= $session->getCustomer()->getData('lastname').'!';
    	}else{
    		$magento_message .= "Guest!";
    	}
    
    # Initiate Blocks
    $linksBlock = $magento_block->createBlock("page/template_links");
    
    $checkoutLinksBlock = $magento_block->createBlock("checkout/links");
    $checkoutLinksBlock->setParentBlock($linksBlock);
    
    $wishlistLinksBlock = $magento_block->createBlock('wishlist/links');
    $wishlistLinksBlock->setParentBlock($linksBlock);
    
    # Add Links
    $linksBlock->addLink($linksBlock->__('My Account'), 'customer/account', $linksBlock->__('My Account'), true, array(), 10, 'class="first"');
    $wishlistLinksBlock->addWishlistLink();
    $checkoutLinksBlock->addCartLink();
    $checkoutLinksBlock->addCheckoutLink();
    
    if ($session->isLoggedIn()) {
        $linksBlock->addLink($linksBlock->__('Log Out'), 'customer/account/logout', $linksBlock->__('Log Out'), true, array(), 100, 'class="last"');
    } else {
        $linksBlock->addLink($linksBlock->__('Log In'), 'customer/account/login', $linksBlock->__('Log In'), true, array(), 100, 'class="last"');
    }
    ?>
    echo $magento_message.''.$linksBlock->renderView().'';
    
    ?>
    
    <!-- End of Magento's custom greeting -->
    

    Funny thing is when I first got this I removed that section and the links came up but but know if i remove it i get “Call to a member function createBlock() on a non-object in…”. Looks like something else might have been changed.

    Thanks,
    Mark

    Reply

    • Richard Feraro
      Jul 14, 2010 @ 15:10:12

      Is this this the first time you used Magento (just asking)?
      Read here on how to call isLoggedIn() method of Magento.

      Reply

  22. Bill
    Jul 14, 2010 @ 22:34:45

    Thanks for this awesome extension. I’m wondering about the customer/user tables within the two applications, does it interfere with rights assigned from the backend to the logged in user of either app? Is a customer that checks out a wp user and if not how could we sync the two?

    Reply

    • Richard Feraro
      Jul 15, 2010 @ 22:45:24

      Hi Bill, thanks for the feedback.

      Regarding your question, the plugin does not interfere with whatever access rights and permission assigned to the user in both application by default. Since the plugin somewhat provides you with raw access to Mage object, it can however modify a user’s permission through that.

      The user by default isn’t logged in automatically on Magento when they access and logged in to WordPress. In order to do that, you can use the Mage object also to login the user in Magento automatically upon logging in to WordPress like my example in the other article I posted last April. You might wanna read my reply to a user named Jim because it could be relevant to what you want to achieve in your application. Check it here.

      Reply

  23. Bill
    Jul 17, 2010 @ 03:10:42

    Richard,

    Thank you for your response. I was able to configure WordPress as the root domain and Magento as a subdirectory or domain.com/shop. I’ve installed the mage enabler and got an error message until I made the adjustments to magento’s functions.php. My problem is creating a subscriber in WordPress does not create the customer in magento and the code example on running magento’s session within wordpress does not pull magento’s header or anything into it.
    Wordpress say’s it see the mage.php. Do I have to make any further code adjustments, scowering your blog I’m getting a little confused with exactly what to modify. My idea is to run a blog with buddypress plugin and a magento shop. I need to have a wordpress subscriber created once a customer creates an account during checkout. I’d also like to have a magento customer created when a subscriber creates an account from wordpress. You mentioned I could use a single table for customers/subscribers but this maybe a little beyond me. Synchronizing Magento’s customers to WordPress subscribers would be fine handled because not all subscribers would be customers. Can you see any way to accomplish this? Thanks again for your assistance.

    Reply

  24. Bill
    Jul 18, 2010 @ 21:27:49

    Ok I was confused. I thought this plugin would enable WordPress to Magento capability for a normal WordPress user. It would be nice if you could package up some type of communication between the two systems that would allow a normal WordPress user to use Magento’s capabilities within WordPress. I’m not putting you down at all for this contribution it’s just a little confusing and time consuming to implement. Having a single sign on between the two applications would be nice along with passing customers, orders and content between the two frameworks.

    Reply

    • Richard Feraro
      Jul 18, 2010 @ 22:37:39

      I think you’re looking for a different plugin and Mage Enabler isn’t for you. Like what you said, you want to use WordPress to your Magento. That’s not what Mage Enabler is for. What the plugin does is to use Magento into WordPress using its session not the other way around. Lazzymonks plugin for Magento us what you need.

      Reply

  25. Bill
    Jul 18, 2010 @ 21:31:23

    Ok maybe not the actually content because each application does quite well with managing it along. I’d just like to be able to pass customers that signup from the Magento checkout through to WordPress as subscribers or other role.

    Reply

    • Richard Feraro
      Jul 18, 2010 @ 22:43:31

      Mage Enabler doesn’t do that. If your primary is purpose is to let Magento do the managing of user roles from Magento to WordPress, better look for a Magento plugin in Magento Connect.

      Reply

  26. Chris
    Jul 19, 2010 @ 23:27:59

    I’m trying to get your plugin to work but keep getting the invalid url message. I have magento set up as a root install and wordpress in installed in a root folder.

    They are both installed in their own databases.

    The url for the mage file is http://mywebdomain.com/app/Mage.php.

    Should they be sharing the same database? Is there a problem with the way they are installed?

    Thanks in advance.

    Reply

    • Richard Feraro
      Jul 21, 2010 @ 02:42:07

      I’m trying to get your plugin to work but keep getting the invalid url message. I have magento set up as a root install and wordpress in installed in a root folder.

      They are both installed in their own databases.

      The url for the mage file is http://mywebdomain.com/app/Mage.php.

      You need the local absolute URL not the web address.

      Should they be sharing the same database? Is there a problem with the way they are installed?

      The database has nothing to do with your error. The plugin only need an absolute URL.

      Look or search for the word “absolute” in the comments area in this page so you’ll have an idea what is Mage.php’s absolute URL.

      Reply

  27. Chris
    Jul 21, 2010 @ 03:42:17

    Hey Richard,

    I think I got the right url, i’m now getting this error message;

    Cannot redeclare __autoload() (previously declared in /home/mysite/public_html/wordpress/wp-content/plugins/connect2MAGE/connect2MAGE.php:52) in /home/mysite/public_html/app/code/core/Mage/Core/functions.php on line 69

    Reply

    • Richard Feraro
      Jul 21, 2010 @ 10:21:25

      That’s not my plugin. It’s connect2Mage like what the error shows. Also, I would expect the same error with any plugin that uses the same application (Magento) since they both utilize the same function. Your error says __autoload() is already declared because you’re using two plugins that links to the same single external application.

      Reply

  28. Chris
    Jul 21, 2010 @ 22:17:45

    sorry about that, here’s the error w/out the other plugin activated:

    Fatal error: Cannot redeclare __() (previously declared in /home/mysite/public_html/wordpress/wp-includes/l10n.php:96) in /home/mysite/public_html/app/code/core/Mage/Core/functions.php on line 96

    Reply

    • Richard Feraro
      Jul 21, 2010 @ 22:35:14

      It’s the function collision between Magento and WordPress. Read the article above, the solution is found in the first paragraph of ‘How to use the plugin?’ section with a link provided on how to fix it.

      Reply

      • Chris
        Jul 22, 2010 @ 04:49:03

        The way I understood that paragraph was that anything that your previous posts stated to do were not required since t you released the plugin. Might make it more clear if you added it to the read me file. Thanks for your help.

        Reply

        • Richard Feraro
          Jul 23, 2010 @ 04:50:58

          I think you misunderstood that phrase. It was never mentioned in any of my post that “that anything that your previous posts stated to do were not required since t you released the plugin“. If you read the article and comments here, you will know that it is referring to the code below which was mentioned in this page too (see comment):

          ...
          require_once 'app/Mage.php';
          umask(0);
          //not Mage::run();
          Mage::app('default');
          ...

          Maybe you were referring to this comment too? The tweak mentioned here is the function magento() added in the WordPress functions.php which was the original code before the plugin even existed. That function magento() also contains the very same code:

          ...
          require_once 'app/Mage.php';
          umask(0);
          //not Mage::run();
          Mage::app('default');
          ...

          The plugin as stated in the description, requires PHP programming. That’s why I don’t see the point why would I tell the readers here to ignore the Magento/WordPress related articles I’m tirelessly posting here in my blog.

  29. Chris
    Jul 21, 2010 @ 22:34:00

    update: ok i deleted the line 96 which is the translate line you have crossed out above response,

    “If you’re already using the Mage Enabler plugin, you don’t have to edit and add the tweak to functions.php anymore since the plugin already does that for you. You still need to get rid of the Magento’s __() translation function though.”

    Thanks for your patience, now I to actually get the cool stuff happening….

    Reply

  30. Jason Demant
    Jul 26, 2010 @ 10:17:55

    Hi Richard,

    Working on changing the way I integrate Magento and WordPress to use your new plugin and I’m running into a (hopefully easy-to-fix) issue.

    I’ve installed the plugin, filled in the absolute path to the Mage.php (File is accessible!). In one of my pages, at the very top of the file I’ve added the following:

    if(class_exists(‘Mage’)){
    echo “YES”;
    }

    And nothing happens, apparently the Mage class doesn’t exist. What am I missing?

    I’m using WP 3.0 and Magento 1.4.0.1.

    Thanks as always!
    Jason

    Reply

    • Richard Feraro
      Jul 26, 2010 @ 11:13:21

      Hi Jason :)

      When you say ‘nothing happens’, does it mean the page is blank or the rest of the page shows except the word ‘YES’?

      Reply

  31. Jason Demant
    Jul 26, 2010 @ 12:49:13

    Sorry, should have been clearer. The rest of the page shows, except the word “YES”.

    Reply

    • Jason Demant
      Jul 26, 2010 @ 17:28:46

      Hey Richard,

      I was running into this issue locally, so I decided to test further to see if I could replicate the issue on my server. And oddly, it works fine on my server. It’s strange that it doesn’t work locally. I’m running Xampp locally, but have never really run into an issue where something doesn’t work locally, but works online. If you happen to have any thoughts, would definitely appreciate it.

      Thanks again,
      Jason

      Reply

      • Richard Feraro
        Jul 26, 2010 @ 18:35:40

        I do have several setup of different version of WordPress including Thelonious. All of them are using Mage Enabler plugin running in XAMPP for Windows (XP). Hmm, could it be a cache issue? Is the file you’re using a template file or some built-in WordPress core files?

        Reply

    • Richard Feraro
      Jul 26, 2010 @ 18:31:52

      Can you check if it created a cookie named ‘frontend’ using Firefox?

      Reply

      • Jason Demant
        Jul 30, 2010 @ 11:33:38

        Hi Richard,

        Unfortunately, still not working locally. I made sure the cache wasn’t an issue and created an else clause to make sure the page outputs at least something and it does…what I put in my else clause.

        The file I’m running is a template file. I create a “Page” in WordPress and then choose the template file as the template. Which is the exact thing I do on my live server, which oddly works.

        I checked to see if a cookie is being created in Firefox and it’s not. Just to double-check, I looked at the page online and it is creating a cookie. Very strange!

        If you have any other recommendations, please let me know.

        Thanks for your help as always!

        Jason

        Reply

        • Richard Feraro
          Jul 30, 2010 @ 17:39:55

          Hmm, that’s weird. If you get the ‘File is accessible!’ message in the admin, it means Mage object is existing like what the plugin code says:

          $message = ( class_exists( 'Mage' ) ) ? 'File is accessible!' : 'Mage object not found!';

          Try doing the code below on the same page your working on. Is should show an array of all the classes that has been declared.

          echo "<pre>";
          print_r(get_declared_classes());
          echo "</pre>";

          Do this code also. It will generate the cookie named ‘frontend’. If it does then it should be working properly.

          if(class_exists('Mage')){
          	// Instantiate session and generate needed cookie
          	Mage::getSingleton('core/session', array('name' => 'frontend'));
          }

  32. Jason M
    Jul 26, 2010 @ 18:18:48

    Hi Richard,

    I tried searching everywhere and can’t understand why I keep getting “invalid Url” it’s maddening. I’ve tried the only absolute url i know which is “http://mysite.com/public_html/shop/app/mage.php” but it still will not work. I know someone else asked the same question, but I couldn’t find a concrete example of what it should look like. I even checked out the post on wordpress.org that you replied to, like 2 hours ago, but that didn’t work either. I would love to get this working:)

    Reply

    • Richard Feraro
      Jul 26, 2010 @ 19:02:25

      Try this:

      Navigate to your Magento installation. Locate and open the .htaccess file using a text editor:
      /magento/app/.htaccess

      Comment out the content per line. It should look like something similar below:
      #Order deny,allow
      #Deny from all

      We did this to disable the restriction in viewing files under this directory. You MUST restore this later.

      Create a file under the same directory and rename it with a .php extension name. Open this file and add the following lines:

      <?php
      phpinfo();

      Save the file and access it via browser (http://your.magento.com/app/your-file-here.php).
      Locate the line that says:

      SCRIPT_FILENAME C:/xampp/htdocs/magento/app/your-file-here.php

      Just replace your-file-here.php with Mage.php and copy the whole URL to the Mage Enabler setting textbox and hit Save.

      DO NOT FORGET TO DO THIS
      Restore your .htaccess to its original content like the one below or everyone will be able to access your Mage.php publicly:
      Order deny,allow
      Deny from all

      Delete your-file-here.php that you created earlier. The content of this file will expose most of your Apache server settings.

      Reply

  33. Jason M
    Jul 26, 2010 @ 19:06:25

    I Fixed the absolute URL, Now I get a fatal error. What could this be from? I don’t have any SEO plugin installed either.
    Fatal error: Uncaught exception ‘Exception’ with message ‘Warning: include(All/In/One/SEO/Pack.php) [function.include]: failed to open stream: No such file or directory in /home

    Reply

    • Richard Feraro
      Jul 26, 2010 @ 19:17:47

      It doesn’t show anything related to Mage Enabler plugin or even Magento itself. Is that the whole error?

      Reply

      • Jason M
        Jul 27, 2010 @ 08:04:44

        I get the following fatal error after successfully linking Mage.php and after I’ve followed all the instructions in this post. I’m useing WP 3.0 and Magento 1.4. If I comment whatever is at line 248 on the functions php the error goes away but loging into mageto with WP credentials doesn’t work. Thank you for checking it out.

        Fatal error: Uncaught exception ‘Exception’ with message ‘Warning: include(All/In/One/SEO/Pack.php) [function.include]: failed to open stream: No such file or directory in /home/myserver/public_html/shop/lib/Varien/Autoload.php on line 93′ in /home/myserver/public_html/shop/app/code/local/Mage/Core/functions.php:248 Stack trace: #0 /home/myserver/public_html/shop/lib/Varien/Autoload.php(93): mageCoreErrorHandler(2, ‘include(All/In/…’, ‘/home/myserver/…’, 93, Array) #1 /home/myserver/public_html/shop/lib/Varien/Autoload.php(93): Varien_Autoload::autoload() #2 [internal function]: Varien_Autoload->autoload(‘All_in_One_SEO_…’) #3 /home/myserver/public_html/wp-content/themes/Apz/functions/admin-functions.php(1097): spl_autoload_call(‘All_in_One_SEO_…’) #4 /home/myserver/public_html/wp-content/themes/Apz/header.php(5): woo_title() #5 /home/myserver/public_html/wp-includes/theme.php(1086): require_once(‘/home/myserver/…’) #6 /home/myserver/public_html/wp-includes/theme.php(1062): load_t in /home/myserver/public_html/shop/app/code/local/Mage/Core/functions.php on line 248

        Reply

        • Richard Feraro
          Jul 27, 2010 @ 16:18:28

          The All in One SEO Pack plugin for WordPress seems to conflict with Mage Enabler. There’s an spl_autoload_call(‘All_in_One_SEO_…’) in your theme files that triggers the error. I’ll investigate on the matter and see what can be done.

        • Richard Feraro
          Jul 27, 2010 @ 16:41:42

          I installed the All in One SEO Pack and activated both plugins but didn’t get the same error that you have. See image below:

          All in One SEO Pack plugin with Mage Enabler enabled

    • Richard Feraro
      Jul 27, 2010 @ 16:10:59

      Your complete error shows that you have the All in One SEO Pack installed.
      .../home/myserver/public_html/wp-content/themes/Apz/functions/admin-functions.php(1097): spl_autoload_call(‘All_in_One_SEO_…’)

      Reply

  34. Joseph
    Aug 10, 2010 @ 06:10:33

    Greetings Richard,

    Thanks for developing Mage Enabler! It looks like it’s exactly what I’m looking for.

    Since I’m a stronger designer than developer, I was hoping you could give me some advice…

    I want to create a store within a WP website. By using Mage Enabler, will I be able to conduct transactions through WP, or will I have to link visitors to a Magento site? I guess I’m trying to figure out what comes first, the Magento site or WP?

    Do you happen to have a step by step post on building a WP and Magento store? How about any link to sites that are doing that?

    THANKS FOR YOUR TIME AND CONSIDERATION!!! Truly appreciate it!

    Best
    Joseph Rubio (Los Angeles)

    Reply

    • Richard Feraro
      Aug 10, 2010 @ 16:08:02

      Hello Joseph, thanks for taking time checking out my blog. Regarding your question, extending Magento to WordPress using its Mage object is like doing php programming and communicating with a third-party’s application API. In this case, you’re dealing with the Mage class and using its methods to do similar tasks present in the actual Magento instance to your external site (WP). What Mage Enabler does is to allow that Mage object to be instantiated within you WP, making all Magento’s methods accessible from within your blog. That why it is possible to use all the functionality mentioned in the Magento documentation.

      Your setup may vary from one project to another. At one instance you may just need to run a WordPress as front end and Magento’s Admin panel so the default Magento front end isn’t used. Sometimes you may want to use both application’s front end, only getting portions of the functionality from Magento. Either way, it is possible. I still don’t have a step by step converting a WP into a full E-commerce site using Magento but there’s already a lot of blogs, post and articles dealing with manipulation of Mage object by including Mage.php within an external site (which is what Mage Enabler does.)

      Reply

    • Richard Feraro
      Aug 10, 2010 @ 21:30:43

      By the way, I moved your comment from the About section to this post since it’s about Mage Enabler.

      Reply

  35. Vali
    Aug 22, 2010 @ 13:14:37

    Richard, your plugin is exactly what i was looking for for some time now.
    What I am trying to to is to insert a product page into a blog post or a blog page. As i am not an experienced Magento developer, i was thinking that you might help me a bit with this one. Let’s say i want to insert the content of product with id 1000. Could you show a few line of code of how to do that from inside wordpress after installing your plugin?
    Thanks

    Reply

  36. Vali
    Aug 25, 2010 @ 04:22:31

    Richard , I want to thank you for your help and for developing this great plug in. It saved me a lot of time and effort .

    Reply

  37. Christopher
    Sep 01, 2010 @ 18:22:24

    Hi Richard,

    A genius plugin for sure. Thank you for sharing it.

    Some feedback though. You have hard-coded the ‘default’ store code into the plugin. My magento site does not have a default store – it looks a bit like this.

    website 1 (private), store 1 code: private_nbno
    website 1 (private), store 2 code: private_engb
    website 2 (wholesale), store 1 code: wholesale_nbno
    website 2 (wholesale), store 2 code: wholesale_engb

    So I get a fatal error when the following code is run:
    // Initialize Magento
    Mage::app( 'default' );

    There ought to be an option to specify a non-default store code in the Mage Enabler settings page.

    Cheers

    Reply

    • Richard Feraro
      Sep 01, 2010 @ 19:49:51

      You’re right. Temporarily, you may edit the code where the Mage::app() is declared. I’ll do an update for the plugin for that purpose :) Thanks for the feedback.

      Reply

    • Richard Feraro
      Sep 01, 2010 @ 20:04:41

      Are the following codes being used as Store View Codes?
      private_nbno, private_engb, wholesale_nbno, wholesale_engb

      Reply

      • Christopher
        Sep 01, 2010 @ 20:30:47

        My bad, yes they are store view codes rather than the store codes

        Reply

Leave a Reply