How to integrate X-Cart store and WordPress blog
If you would like to have WordPress blog displayed on your X-Cart store pages, that is rather easy to do. You can find step-by-step instructions for the integration below:
1. First, you need to make sure your server rewrite engine is enabled, then you need to add the following rules to the .htaccess file (located in X-Cart folder on your server):
RewriteEngine On RewriteRule ^blog/(wp-login\.php|wp-admin|wp-includes|wp-content|wp-comments-post\.php|wp-cron\.php|xmlrpc\.php)(.*)$ blog/$1$2 [QSA,L] RewriteRule ^blog/(.*).(css|js|gif|jpg|png|html|php)$ blog.php?incl_file=$1.$2 [QSA,L] RewriteRule ^blog/.*$ blog.php?url=$1 [QSA,L] RewriteRule ^blog$ blog.php [QSA,L]
You should insert it before any X-Cart .htaccess rules
Please note that these rules imply that X-Cart store is installed in the server root directory while WordPress software is installed in a blog sub-directory (e.g. X-Cart store is available at domain.com while WordPress is available at domain.com/blog).
All of the blog requests are forwarded to a new “blog.php” file - it needs to be created in X-Cart root directory on the server:
<?php @ini_set('memory_limit', '64M'); $xcart_dir = rtrim(realpath(dirname(__FILE__)), DIRECTORY_SEPARATOR); $use_wp_file = 'index.php'; if ($_GET['incl_file']) { $incl_file = preg_replace('/\.\./', '', $_GET['incl_file']); $file = $xcart_dir.'/blog/'.$_GET['incl_file']; $path_info = pathinfo($file); if (in_array($incl_file, array('wp-comments-post.php'))) $use_wp_file = $incl_file; elseif (is_file($file)) { if ($path_info['extension'] == 'js') { header('Content-Type: application/x-javascript'); echo file_get_contents($file); } elseif($path_info['extension'] == 'css') { header('Content-Type: text/css'); echo file_get_contents($file); } elseif($path_info['extension'] == 'gif') { header('Content-Type: image/gif'); echo file_get_contents($file); } elseif($path_info['extension'] == 'png') { header('Content-Type: image/png'); echo file_get_contents($file); } elseif($path_info['extension'] == 'jpg') { header('Content-Type: image/jpeg'); echo file_get_contents($file); } elseif($path_info['extension'] == 'html') { header('Content-Type: text/html'); echo file_get_contents($file); } die(); } elseif($path_info['extension'] == 'html') { # possible that the clean url has got the html extension $url = $_GET['url'] = $_GET['incl_file']; } else die(); } ob_start(); chdir('blog/'); include_once $use_wp_file; $content = ob_get_contents(); chdir($xcart_dir); ob_end_clean(); if (in_array(trim($_SERVER['REQUEST_URI'], '/'), array('blog/feed', 'blog/feed/rss', 'blog/feed/atom')) || $_GET['feed']) { echo $content; die; } define('CONTENT_VAR', $content); include "./auth.php"; $content = preg_replace('/\[if gte mso 9\].*\[endif\]/eUims', '', CONTENT_VAR); if (!preg_match("/\<head.*\>.*\<title\>(.*)\<\/title\>(.*)\<\/head\>.*\<body.*\>.*\<h1 id=\"header\".*\<\/h1\>(.*)\<\!-- end sidebar --\>.*\<\/body\>/Uims", $content, $out)) preg_match("/\<head.*\>.*\<title\>(.*)\<\/title\>(.*)\<\/head\>.*\<body.*\>.*\<div id=\"header\" role=\"banner\"\>.*\<hr \/\>(.*)\<hr \/\>.*\<div id=\"footer\" role=\"contentinfo\"\>.*\<\/body\>/Uims", $content, $out); if (!$out) preg_match("/\<head.*\>.*\<title\>(.*)\<\/title\>(.*)\<\/head\>.*\<body.*\>.*\<div id=\"header\">.*\<\!\-\-\ #header\ \-\->(.*)<div id=\"footer\" role=\"contentinfo\"\>.*\<\/body\>/Uims", $content, $out); if (!$out) preg_match("/\<head.*\>.*\<title\>(.*)\<\/title\>(.*)\<\/head\>.*\<body.*\>.*\\<\/header\>(.*)\<\!\-\- #main \-\-\>/Uims", $content, $out); if (!$out) preg_match("/\<head\>.*\<title\>(.*)\<\/title\>(.*)\<\/head\>.*\<body id\=\"error\-page\"\>(.*)\<\/body\>/Uims", $content, $out); if (!$out) preg_match("/\<head\>.*\<title\>(.*)\<\/title\>(.*)\<\/head\>.*\<\!\-\-\ #masthead\ \-\-\>(.*)\<\!\-\-\ #main\ \.wrapper\ \-\-\>/Uims", $content, $out); preg_match_all("/(<link.*>)/", $out[2], $links); preg_match_all("/(<style.*<\/style>)/ims", $out[2], $styles); preg_match_all("/<meta.*(name|property)=\"(.*)\".*content=\"(.*)\"/Uims", $out[2], $meta); preg_match_all("/(<script.*<\/script>)/Uims", $out[2], $jss); $wordpress_content = $out[3]; if(is_array($jss[1])) foreach($jss[1] as $js) $js_content .= $js."\n"; $wordpress_content = $js_content."\n".$wordpress_content; if(is_array($links[1])) foreach($links[1] as $link) $css_content .= $link."\n"; if(is_array($styles[1])) foreach($styles[1] as $css) $css_content .= $css."\n"; $wordpress_content = $css_content."\n".$wordpress_content; $wordpress_title = $out[1]; # for X-Cart 4.4 require $xcart_dir."/include/common.php"; # for older versions #require $xcart_dir."/include/categories.php"; if ($active_modules["Manufacturers"]) include $xcart_dir."/modules/Manufacturers/customer_manufacturers.php"; if ($active_modules["Bestsellers"]) include $xcart_dir."/modules/Bestsellers/bestsellers.php"; $add_meta = array(); if (is_array($meta)) foreach($meta[2] as $ind=>$val) { if ($val == 'keywords') $_meta_keywords = trim($meta[3][$ind]); elseif($val == 'description') $_meta_descr = trim($meta[3][$ind]); elseif($meta[1][$ind] == 'property' && preg_match('/^og\:.*$/', $val)) $add_meta[$val] = trim($meta[3][$ind]); } $smarty->assign('page_data', array('metatitle' => $wordpress_title, 'metadescr' => $_meta_descr, 'metakeywords' => $_meta_keywords, 'additional_meta' => $add_meta)); unset($location); $location[] = array($wordpress_title); $smarty->assign('location', $location); $smarty->assign('wordpress_content', '<div style="position:relative">'.$wordpress_content.'</div>'); $smarty->assign('main', 'wordpress'); func_display('customer/home.tpl', $smarty); ?>
Using this code WordPress pages content is generated and parsed. We extract the meta information and the content body from WordPress system, and this data is then used by X-Cart default Smarty templates.
2. As for templates, first we should include a new template for the blog content into the “skin/common_files/customer/home_main.tpl” (“skin1/customer/home_main.tpl” in older versions) file (in X-Cart folder) by adding the following 2 lines:
{elseif $main eq "wordpress"} {include file="customer/main/wordpress.tpl"}
It's better to add the code to the end of the file, e.g. in this way:
{elseif $main eq "view_results"} {include file="modules/Survey/customer_view_results.tpl"} {elseif $main eq "wordpress"} {include file="customer/main/wordpress.tpl"} {else} {include file="common_templates.tpl"} {/if}
Also we should create a new template for the WordPress blog content - “skins/common_files/customer/main/wordpress.tpl” (“skin1/customer/main/wordpress.tpl” in older versions) - with the following lines:
{capture name=dialog} {$wordpress_content} {/capture} {include file="dialog.tpl" title=$page_data.metatitle content=$smarty.capture.dialog extra='width="100%"'}
3. In addition, it is worth showing WordPress meta tags in default X-Cart meta tags.
The multiple skins were added in X-Cart 4.4, so you should modify the skin/<your skin name> templates or skin/common_files templates if the template is missed in your skin. It's better to enable the webmaster mode to see which template is used. For the meta tags you should find the following code in customer/meta.tpl in your skin:
{if $printable} <meta name="ROBOTS" content="NOINDEX,NOFOLLOW" /> {else}
and replace it to the following one:
{if $printable} <meta name="ROBOTS" content="NOINDEX,NOFOLLOW" /> {elseif $main eq 'wordpress'} <meta name="description" content="{$page_data.metadescr|truncate:"500":"...":false|escape}" /> <meta name="keywords" content="{$page_data.metakeywords|truncate:"500":"":false|escape}" /> {if $page_data.additional_meta} {foreach from=$page_data.additional_meta key=prop item=val} <meta property="{$prop}" content="{$val}" /> {/foreach} {/if} {else}
For the title tag displaying, find the following code in the customer/service_head.tpl
{get_title page_type=$meta_page_type page_id=$meta_page_id}
and replace it to the following lines:
{if $main eq 'wordpress'} <title>{$page_data.metatitle}</title> {else} {get_title page_type=$meta_page_type page_id=$meta_page_id} {/if}
In X-Cart 4.2.x version you need to change the skin1/customer/meta.tpl template and insert the following code:
{if $printable} <meta name="ROBOTS" content="NOINDEX,NOFOLLOW" /> {elseif $main eq 'wordpress'} <meta name="description" content="{$page_data.metadescr|truncate:"500":"...":false|escape}" /> <meta name="keywords" content="{$page_data.metakeywords|truncate:"500":"":false|escape}" /> {if $page_data.additional_meta} {foreach from=$page_data.additional_meta key=prop item=val} <meta property="{$prop}" content="{$val}" /> {/foreach} {/if} {else} {meta type='description' page_type=$meta_page_type page_id=$meta_page_id} {meta type='keywords' page_type=$meta_page_type page_id=$meta_page_id} {/if}
You can see here additonal {elseif}…{else} section.
In order to change a page title in previous X-Cart versions it is required to change the “skin1/customer/home.tpl” template in X-Cart by inserting the following lines:
<title> {if $page_data.metatitle} {$page_data.metatitle} {else} ... {/if} </title>
where the ”…” value is the default code inside the ”<title>” tag.
For the same purpose the “skin1/meta.tpl” template in X-Cart should be changed in the following way:
{if $page_data.metadescr} {assign var="_meta_descr" value=$page_data.metadescr} {/if} {if $page_data.metakeywords} {assign var="_meta_keywords" value=$page_data.metakeywords} {/if}
The code should be added below the following lines:
{assign var="_meta_descr" value=""} {assign var="_meta_keywords" value=""}
Add-ons
Ability to place the RSS feed on the X-Cart side This module allows to show a few last posts from the blog on X-Cart side, for example in menu.
X-Cart - Wordpress integration doesn't work with some of the modules The ability to run the blog integration with some of WordPress modules that break the code execution. The php save mode must be “off”.
Possible problems
Problems with X-Cart 4.4
X-Cart 4.4 architecture slightly differs from the previous software versions. Thus you need to remove the following line from the blog.php:
require $xcart_dir."/include/categories.php";
and replace it with
require $xcart_dir."/include/common.php";
The templates are also a little bit different in 4.4.x. Customer templates are located in the skin/common_files/customer/ directory, so you will find the home_main.tpl here.
Problem with styles
Since X-Cart 4.3.x-4.4.x versions, WordPress and X-Cart styles are intersected. You can find #header and #footer styles in both software packages. All of the duplicates should be removed from WordPress blog styles. It can be done without any doubt since these styles are not used anymore after the integration (a header and a footer in the blog are replaced with X-Cart ones).
Empty content
Sometimes this script can not parse WordPress pages properly in order to find meta and body information. Such problem may be related to your WordPress blog version or design changes performed to WordPress skin. In this case it will be required to review “blog.php” file and find suitable preg_match expression.
PS: The preg_match is able to parse the default WP skins, please check that you are using it. The default skin is “Twenty Ten by the WordPress team”.
Empty content in 4.4.x version
The other reason for empty content in 4.4 version is skin modification. Please try to use the following modification of the skin/common_files/customer/home_main.tpl (instead of the suggested above):
{elseif $main eq "wordpress"} {$wordpress_content}
Redirect loop
You need to make sure that there are no mod rewrite conditions set up in the blog/.htaccess file, otherwise they will break the integration rules.
Also http WordPress and X-Cart settings should be similar. So if http://www.example.com is used in X-Cart settings, you just can not use http://example.com in WordPress settings.
Should you have any questions, feel free to contact us . We will be glad to help you.
Integration doesn't work with some of the module
This issue was moved to the separate article, because the additional changes in the blog.php are required. Also the server configuration might be required.
X-Cart - Wordpress integration doesn't work with some of the modules
Links are wrong, base href issue
If you have not enable the clean url, or have got the version prior the 4.4.x, you should insert the “base href” tag to the head section of the page:
<base href="{$catalogs.customer}/" />
Examples
http://www.lifetimecollective.com/blog/
http://www.saxxapparel.com/blog/
http://www.brooklyncruiser.com/blog/
For now there are more then 50 known installations.
Changelog
15 Mar 2013 - Update for the new WordPress skin
19 Dec 2012 - Fix .htaccess for xmlrpc.php
21 May 2012 - Added the ability to show the og:title, og:description, og:rl and other og:* meta properties
21 May 2012 - Note for the 4.4 skin modification was added
14 Sep 2011 - Fix cron
14 Sep 2011 - The WP errors are displayed now if required
15 Aug 2011 - The .htaccess was changed to be suit for older versions
14 Jul 2011 - The new WP skin/version was added
15 Apr 2011 - The modules problem was solved.
22 Oct 2010 - The 4.4.x notes were added.
11 Oct 2010 - The default skin of the WordPress 3.x was added to the preg_match
09 Oct 2010 - New note abut X-Cart 4.4. Small changes are required in blog.php
01 Jul 2010 - Problem with feed and the disabled clean urls.
27 May 2010 - Additional note about X-Cart/WordPress styles.
14 May 2010 - Fix for possible html extension in the clean url.
Other things
If you meet any problem with the code above, please contact us or pay for the installation.