We’ve had several questions lately about how you can use PHP includes in your datafeed web service client to do things like add standard site-wide content to your datafeed template.
Note: Due to some recent changes in the way the datafeed client creates SEO friendly URLs, we’ve changed the main datafeed client script name from “shop” to “shop.php”. To cover both versions we’ll refer to this script as “shop” or “shop.php” below.
Here’s how you can accomplish this:
- Look for your “shop” or “shop.php” script which is in the same folder as the “template.html” file.
- Make a backup of both the “shop” or “shop.php” script and the “template.html” file just in case you want to revert back to the original.
- Open the “shop” or “shop.php” file up in a text editor and add the following right below the opening <?php code:
ob_start(); include('/path_to_include/include.php'); // Replace with actual path and file name. $my_include_content = ob_get_contents(); ob_end_clean(); - Look for the block of code that looks like this:
$strHTML = eregi_replace('\[PAGE_TITLE\]', $page_title, $strHTML); $strHTML = eregi_replace('\[PAGE_KEYWORDS\]', $page_keywords, $strHTML); $strHTML = eregi_replace('\[PAGE_DESCRIPTION\]', $page_description, $strHTML); $strHTML = eregi_replace('\[PAGE_NAVIGATION\]', $page_navigation, $strHTML); $strHTML = eregi_replace('\[PAGE_CONTENT\]', $page_content, $strHTML); $strHTML = eregi_replace('\[HIT_TRACKING\]', $hit_tracking, $strHTML); - Add the following line right below that block of code:
$strHTML = eregi_replace('\[MY_INCLUDE_CONTENT\]', $my_include_content, $strHTML); - Save your “shop” or “shop.php” script.
- Open up your “template.html” file and add [MY_INCLUDE_CONTENT] to the html where you want that content to show.
- Save your template.html file.
- Upload your files to your server.
- If this isn’t the first time you’ve uploaded changes and/or viewed your datafeed pages make sure you clear your cache by logging into your account and going to ‘Tools -> Edit Current Datafeeds’ and then click on the ‘Clear Page Cache’ link next to your datafeed subscription in the list.
** These instructions assume that your PHP include just echo’s out a block of html content. If your include script places your html content within PHP variables then all you would need to do is simply include your script from within the “shop” or “shop.php” file, add eregi_replace() function calls (See example above) for each of the variables you want to use from within your include script, and then add those place holders (eg. [MY_PLACE_HOLDER]) to your template.html file.
Scott & AvantLink
Note: The following addendum is from June 30, 2009.
The format of our shop.php script has changed significantly since this post was written (in February 2008), however many of the instructions above remain valid – with a few key differences.
First, the suggested code for populating the $my_include_content variable – from ob_start() to ob_end_clean() – should be placed within the getTemplatedContent() function rather than immediately after the opening <?php tags, e.g.:
function getTemplatedContent($strFilePath, $page_title, $page_keywords,
$page_description, $page_navigation, $page_content, $hit_tracking)
{
ob_start();
include('/path_to_include/include.php'); // Replace with actual path and file name.
$my_include_content = ob_get_contents();
ob_end_clean();
if (function_exists('file_get_contents')) {
Next, it is no longer recommended to use the eregi_replace() function to insert content into your template. Instead there’s a branch of logic in the shop.php script that uses the str_ireplace() function if available (it was added in PHP 5) or the preg_replace() function otherwise. So the final block of code that ensures your custom variable gets inserted into the template properly would look like the following:
if (function_exists('str_ireplace')) {
$strHTML = str_ireplace(
array( '[PAGE_TITLE]',
'[PAGE_KEYWORDS]',
'[PAGE_DESCRIPTION]',
'[PAGE_NAVIGATION]',
'[PAGE_CONTENT]',
'[HIT_TRACKING]',
'[MY_INCLUDE_CONTENT]' ),
array( $page_title,
$page_keywords,
$page_description,
$page_navigation,
$page_content,
$hit_tracking,
$my_include_content ),
$strHTML );
}
else {
$page_title = str_replace('$', '\$', $page_title);
$page_keywords = str_replace('$', '\$', $page_keywords);
$page_description = str_replace('$', '\$', $page_description);
$page_navigation = str_replace('$', '\$', $page_navigation);
$page_content = str_replace('$', '\$', $page_content);
$hit_tracking = str_replace('$', '\$', $hit_tracking);
$my_include_content = str_replace('$', '\$', $my_include_content);
$strHTML = preg_replace('#\[PAGE_TITLE\]#i', $page_title, $strHTML);
$strHTML = preg_replace('#\[PAGE_KEYWORDS\]#i', $page_keywords, $strHTML);
$strHTML = preg_replace('#\[PAGE_DESCRIPTION\]#i', $page_description, $strHTML);
$strHTML = preg_replace('#\[PAGE_NAVIGATION\]#i', $page_navigation, $strHTML);
$strHTML = preg_replace('#\[PAGE_CONTENT\]#i', $page_content, $strHTML);
$strHTML = preg_replace('#\[HIT_TRACKING\]#i', $hit_tracking, $strHTML);
$strHTML = preg_replace('#\[MY_INCLUDE_CONTENT\]#i', $my_include_content, $strHTML);
}
And that’s it – all of the other original instructions still apply. As always, if you have any questions or comments, please let us know.



1JT VonLunen on Feb 21, 2008 at 3:15 pm:
Thanks for the quick response on this Scott. The first few lines of code are key.
I had to reset the “include_path” right before my include call to get the correct file name because the .php file I using also has additional includes in the code. I kept getting an error that the directory or files don’t exist at “include_path”= /mysite/avant_includes. Here’s an example of what I was able to accomplish(thanks to scott’s help): http://www.nationaloutdoors.net/bc/shop.php/D-1/CampHike.html. You can see recent posts (my content) at the bottom of the page.
2support on Feb 21, 2008 at 3:35 pm:
That’s looking great JT. Your implementation is an excellent example of what can be accomplished using this tool.
Scott
3BJ Baker on Feb 24, 2008 at 5:41 pm:
How do you reset the “include_path”
4support on Feb 25, 2008 at 8:53 am:
You can use code similar to the following to reset the include_path variable from within a script:
$new_path = ‘Whatever additional path you want to include from’;
$current_path = ini_get(“include_path”);
ini_set(“include_path”, “$current_path:” . $new_path);
-David
5Charlie Barks on Sep 18, 2008 at 5:20 pm:
Just a note to mention that I was able to use this code to add multiple PHP Includes in my Template.html. I am not a coder but was able to figure it out so thought I would share in case someone else wishes to do the same thing. My goal was to be able to put my upper navbar and sidenav into the template as a php include so that I could update one file to update all my navigation.
Basically all I did just add the code, as per Scott’s instructions above, two times then change a few variables in the second instance of the code.
For Example in this code below:
ob_start();
include(‘/serversideincludes/sidenav.html’); // Replace with actual path and file name.
$my_include_sidenav = ob_get_contents();
ob_end_clean();
I changed the include path on the second line of code and I also changed the 3rd line from “$my_include_content” to something unique like “$my_include_sidenav”.
The next block of Scott’s code I changed also, changing “CONTENT” and “content” to SIDENAV and sidenav like you see in the code below:
“$strHTML = eregi_replace(‘\[MY_INCLUDE_SIDENAV\]‘, $my_include_sidenav, $strHTML);”
Then you in your template.html you would add the new code “[MY_INCLUDE_SIDENAV]” where you want to second include to be added.
Note: I had a problem with the way that Scott’s code copies and pastes into Dreamweaver and David shared that it was because Wordpress displays this part of the code differently for the single ‘ quote marks. So the easiest way to correct this is to copy and paste the “single ‘ quote marks” from your shop.php code and replace them in the code that you copied and pasted into your shop.php.
It’s really hard to tell the difference in this small single quote mark (not sure what the official name of this symbol is) but I can tell you that the code will not work if you don’t replace them. Certainly those with more coding experience know this but a rookie like me would miss this. So just replace all four of the single ‘ quote marks with ones copied from your original code and it will work.
Thanks Scott and David. Please feel free to correct anything that I didn’t explain properly and thanks for your help.