Last active
August 29, 2015 14:22
-
-
Save justindocanto/42423d28b735a407b812 to your computer and use it in GitHub Desktop.
Use WordPress functions within Magento
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// I do a lot of websites where the client would like the ease of WordPress for a blog and | |
// for regular pages, but want something enterprise level for the e-commerce portion of | |
// their site. In order to service this need, I usually setup a wordpress site as the main | |
// site and then setup Magento as a secondary site at something like example.com/shop/. | |
// Then, I use the following 'hack' in order to use WP elements from within Magento so I | |
// don't have to do twice the work and/or so the client, if they are managing the site | |
// themselves, don't have to change things in two places. For example, you can use WordPress | |
// nav menus within Magento, so that you don't have to maintain 2 menus. You can also call | |
// widget areas, query recent posts for the sidebar, include custom css styling, and so on. | |
// If anybody comes across this and has questions, please email me at [email protected] | |
// STEP 1 - PREVENT __() CONFLICT | |
// WHY: | |
// WP & Magento both create new __() functions on init. If you're loading WP from within Magento, | |
// the WP one will run second & cause an error because that function has already been created. | |
// To prevent this, we can wrap it in a conditional statement that checks if the function exists. | |
// By checking if it exists and not removing the function all together, we are allowing WordPress | |
// to use the function when it's operating normally (when it's not being called within Magento). | |
// In /wp-includes/l10n.php, FIND: | |
function __( $text, $domain = 'default' ) { | |
return translate( $text, $domain ); | |
} | |
// REPLACE WITH: | |
if(!function_exists("__")) { | |
function __( $text, $domain = 'default' ) { | |
return translate( $text, $domain ); | |
} | |
} | |
// STEP 2 - INITIATE WORDPRESS WITHIN MAGENTO | |
// WHY? | |
// First, In order to load just the parts of WP that we want, we define WP_USE_THEMES as false. | |
// Second, we set the global wp variables that are required to initiate wordpress. | |
// Third, we include the file that initiates WordPress. | |
// The last step is to call wp() which sets up all the functions we want access to. | |
// In your magento theme file(s), define the following before you want to use a WP function | |
// You only need to define this once per file | |
define('WP_USE_THEMES', false); | |
global $wp, $wpdb, $wp_query, $wp_the_query, $wp_rewrite, $wp_did_header; | |
require('/ABSOLUTE/PATH/TO/WORDPRESS/DIRECTORY/wp-load.php'); // Replace with correct path | |
wp(); | |
// STEP 3 - USE ANY WORDPRESS FUNCTION YOU'D LIKE | |
bloginfo("title"); // This will echo the title of your WordPress site | |
// I usually test with this one, but you're able to do queries, loops and so on. | |
// Enjoy! | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment