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)(.*)$ 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]
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:
<?
$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($_SERVER['REQUEST_URI'], array('/blog/feed/rss/', '/blog/feed/', '/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);
preg_match_all("/(<link.*>)/", $out[2], $links);
preg_match_all("/(<style.*<\/style>)/ims", $out[2], $styles);
preg_match_all("/<meta.*name=\"(.*)\".*content=\"(.*)\" \/>/", $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];
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";
if (is_array($meta))
foreach($meta[1] as $ind=>$val) {
if ($val == 'keywords')
$_meta_keywords = $meta[2][$ind];
elseif($val == 'description')
$_meta_descr = $meta[2][$ind];
}
$smarty->assign('page_data', array('metatitle' => $wordpress_title, 'metadescr' => $_meta_descr, 'metakeywords' => $_meta_keywords));
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 “skin1/customer/home_main.tpl” 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 - “skin1/customer/main/wordpress.tpl” - with the following lines:
{capture name=dialog}
{$wordpress_content}
{/capture}
{include file="dialog.tpl" title=$_meta_title content=$smarty.capture.dialog extra='width="100%"'}
3. In addition, it is worth showing WordPress meta tags in default X-Cart meta tags.
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}" />
{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 addiitonal {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=""}
Possible problems
Problem with styles
Since X-Cart 4.3.0 version WordPress and X-Cart styles are crossed. You can notice #header or #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 the WordPress skin. In this case it will be required to review “blog.php” file and find suitable preg_match expression.
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.
Examples
http://justbedding.com.au/blog/
http://www.lifetimecollective.com/blog/
http://www.saxxapparel.com/blog/
For now there are more then 50 known installations.
Changelog
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.