27Aug/101

Adding Page Titles to Your WordPress/Magento Integration Using CURL

Branko's solution for integrating WordPress with Magento has proven to be easy and popular. However, we have encountered hosting providers that disallow file_get_contents and needed to find a workaround. So we turned to the CURL library, and in the process we were able to pass WordPress page titles into Magento. Read on for step-by-step instructions.

Adding page titles to your blog is a usability and SEO improvement, so you may want to consider this method even if your integration is working with file_get_contents*. There are four steps to get WordPress page titles into your integrated blog.

Step 1:

Implement Branko's original WordPress integration Branko's original WordPress integration. Remember to back up all your files before making any changes.

Step 2:

In your WordPress theme's functions.php file, replace this code:

public function __construct($url, $markerStartHeader = null, $markerEndHeader = null, $markerStartFooter = null, $markerEndFooter = null)
{
    $this->_content = file_get_contents($url);
    $this->_renderHeader($markerStartHeader, $markerEndHeader);
    $this->_renderFooter($markerStartFooter, $markerEndFooter);
}

With this:

public function __construct($url, $markerStartHeader = null, $markerEndHeader = null, $markerStartFooter = null, $markerEndFooter = null)
{
    //create array for passing to Magento
    $wp_variables = array();
    //read WordPress title into variable array
    $wp_variables['wp_title'] = wp_title('', false, 'right');
    //initialize CURL handle
    $ch = curl_init();
    //set CURL URL
    curl_setopt ($ch, CURLOPT_URL, $url);
    //POST variables to the Magento page
    curl_setopt($ch, CURLOPT_POST, 1);
    //build a key/value string to send to Magento (Passing an array to CURLOPT_POSTFIELDS will encode the data as multipart/form-data, while passing a URL-encoded string will encode the data as application/x-www-form-urlencoded.)
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($wp_variables));
    //return the results as a string
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    //read results into _content
    $this->_content = curl_exec($ch);
    //close CURL handle
    curl_close($ch);

    $this->_renderHeader($markerStartHeader, $markerEndHeader);
    $this->_renderFooter($markerStartFooter, $markerEndFooter);
 }

Step 3:

Now go to /app/design/frontend/default/YOURTHEMENAME/template/page/html/head.phtml.

Replace this (around line 27):

<title><?php echo $this->getTitle() ?></title>

With this:

<?php if ($_POST['wp_title']) { ?>
    <title><?php echo $_POST['wp_title'] ?> &gt; <?php echo $this->getTitle() ?></title>
<?php } else { ?>
    <title><?php echo $this->getTitle() ?></title>
<?php } ?>

Step 4:

Upload /app/design/frontend/default/YOURTHEMENAME/template/page/html/head.phtml and your WordPress theme's functions.php file.

That's it! You should now see your WordPress page titles working in your Magento header.

*You can pass in POST data to a file using file_get_contents, but it is slightly more involved.

About Sean Valencourt

Sean is MagThemes' resident backend developer and JavaScript guru. Sean has been developing in PHP and MySQL for over 8 years, and while he generally adheres to the MVC pattern, he's not averse to hacking Magento to get the right results.
Comments (1) Trackbacks (0)
  1. hi very good blog!
    i wanted to know how the process is adding wordpress into magento but when i clicked on the link to branko’s post, the page did not exist. i’d like to know the steps needed to do this. hope u can help. thanks.


Leave a comment


No trackbacks yet.