Including Magento blocks in a WordPress theme is straight forward, however, the internationalisation feature of both systems gets in the way.
An example
1 2 3 4 5 6 7 8 9 10 11 12 13 |
// File 1 - include_1.php function foo($a) { return $a*2; } // File 2 - include_2.php function foo($b) { return $b+9; } // File 3 - index.php include "include_1.php"; include "include_2.php"; |
File 3 would throw: Fatal error: Cannot redeclare foo()
So, what’s the problem?
Put simply, they both have the same function. Whichever one comes first, PHP raises an error when the second one is defined. In the case of WordPress and Magento, they both have the function __
.
How do I overcome this issue?
Deactivate a Magento function that conflicts with one in WordPress.
This is the easiest way to do it, as otherwise we’d need to modify core WordPress. Magento allows us to over-ride files using a “local” version.
The implemenation
Copy /app/code/core/Mage/Core/functions.php
to /app/code/local/Mage/Core/functions.php
– this file will now be used over the original, and will remain during Magento upgrades.
Open the file at /app/code/local/Mage/Core/functions.php
and locate the function __
function. It should be around line 90, but can vary depending on your Magento version.
1 2 3 4 |
function __() { return Mage::app()->getTranslator()->translate(func_get_args()); } |
This should now become:
1 2 3 4 5 6 |
if (!function_exists('__')) { function __() { return Mage::app()->getTranslator()->translate(func_get_args()); } } |
This tells Magento that it should only actually define the function if it’s not already been defined (by WordPress in our case).
So, how do I now include a CMS static block in my theme?
1 2 3 4 5 6 |
//include Magento require_once '../app/Mage.php'; umask(0); Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID); $staticBlock = Mage::getModel('cms/block')->load('static-cms-block-identifier'); echo $staticBlock->getContent(); |
Image Credit: Ozgurmulazimoglu
Pingback: The Wordpress Experts – Including Magento blocks in a WordPress theme | Web …
Pingback: Including Magento blocks in a WordPress theme | Web … | Magento News