14

Sep

Automatically inserting posts into WordPress from a XML feed

This post will show you how to insert a post into WordPress from a XML feed. I use this to populate my www.caniplay.co.uk website with products from the MediFusion feeds.

 

The posts are not finished when inserted, hence why I make them draft posts. They still need a category assigning to them and any other SEO type stuff I want to add.

 

What I do on www.caniplay.co.uk is attach this code to a hidden page and call it with a start date and end date. This will then add all the products from the feed within that date. I then go through each draft post, tidy it up and publish it to the site.

 

I find this the easiest way to add new posts to my site, as I just go into the drafts folder on WordPress and if there are posts in their I know they need tidying up and adding to the site.

 

UPDATE: I have now added to the code, it now also takes the associated JPG image and uploads that and also links it to the post you are creating. It does a quick check to make sure there is a image (some games have no image associated with them in the feed) and if there is a image URL then it uses it.

 

<?php

$xml = simplexml_load_file("http://www.find-games.co.uk/services/gamesearch.asp?mode=heavy&site=YOURTAGHERE&platform=ps3&category=consoles");

foreach ($xml->children() as $item)
{

	// Create post object
	$my_post = array(
		'post_title' => $item->groupTitle,
		'post_content' => $item->description . '[get_details ASIN="' . $item->id . '"]',
    	'post_status' => 'draft',
    	'post_author' => 1
	);

$new_post = wp_insert_post( $my_post );

if(!empty($item->imageURL))
{
	$file_url = media_sideload_image($item->imageURL,$new_post);
	echo $new_post . ' ' . $item->imageURL . ' ' . $file_url;
}
else
{
	echo $new_post;
}

?>
Facebook Twitter Linkedin Digg Delicious Reddit Stumbleupon Tumblr Posterous Email Snailmail
Fork me on GitHub

28

Jun

Display Voucher Codes using the Buy.at API in PHP

This bit of PHP code will display all the Voucher Codes from the Buy.at network.

 

<?php

$xml = simplexml_load_file("https://users.buy.at/ma/index.php/affiliateVoucherCodes/list?handle=0&filter_status=y&include_pending=1&include_active=1&include_expired=0&prog_id=0&vertical_id=0&orderby=date_added&dir=desc&format=xml&email=USERNAME&password=PASSWORD");

echo '<table>';

echo '<tr>
        	<th scope="col">Retailer</th>
            <th scope="col">Description</th>
            <th scope="col">Voucher Code</th>
            <th scope="col">Start Date</th>
            <th scope="col">End Date</th>
            <th scope="col">Link</th>
        </tr>';

foreach($xml->body->children() as $child)
{
		if($child->status=='Active')
		{
			echo '<tr>';
			echo '<td>' . $child->programme . "</td>";
			echo '<td>' . $child->description . "</td>";
			echo '<td>' . $child->offer_code . "</td>";
			echo '<td>' . $child->start . "</td>";
			echo '<td>' . $child->end . "</td>";
			echo '<td><a href="' . $child->url . '" target="_blank" rel="nofollow" title="Visit website to use voucher code">Use Code</a></td>';
			echo '</tr>';
		}
}

echo '</table>';
?>

 
Remember to change the USERNAME and PASSWORD in the URL line to your own. There are also some parameters you can configure, I have it set to only show non-expired vouchers and in date order.

 
I use this code on my WP site, example here.
 

Facebook Twitter Linkedin Digg Delicious Reddit Stumbleupon Tumblr Posterous Email Snailmail

17

Jun

Fancy Easy Content Unit using JQuery, PHP and RSS

I wrote this as I was bored and fancied having a play with the ECU RSS feed option. Using the RSS feed part of a ECU the results are endless what you can do with it, your not tied to using the standard units that the ECU site supplies.

Get the main HTML file from here. What I did was view the demo in it’s own window, viewed the source code and copied and pasted into my code editor. Then you need to save the file as a .php file.
 

At the very top of the file, add this PHP code.

<?php
$xml = simplexml_load_file("http://www.easycontentunits.com/rss/6553/3/rss2full.rss");
?>

Remember to change the URL to match the easy content unit you want to use.

Then replace the “< div id=’sidebar’ >” with the code below.

<div id="sidebar">
<ul class="spy">
<?php
foreach ($xml--->channel->children() as $item)
{
	if($item->price!='')
	{
		echo '
	<li>';
		echo '<a title="View round" href="#"><img src="' . $item->image . '" alt="" width="70" height="70" /></a>';
		echo '
<h5><a title="View round" href="#">' . substr($item->title, 0, 30) . '</a></h5>
';
		echo '

' . substr($item->description, 0, 60) . '

';

		echo '

<a title="Buy this item" href="#">£' . strip_tags($item->price) . '</a>';
		echo '<a title="Buy this item" href="' . $item->link . '">Buy Now</a>

';
		echo '</li>
';
	}
}
?>
</ul>
</div>

You should then be able to upload the file then and view it, remember its a PHP file, so won’t run locally on your machine. also your host must support PHP (most do). My example is here.
 

If all is ok, you should then be able the put that div and where on page where you would like the ECU to appear.
 

I have been asked if this can be used in WordPress but unfortunately I have no experience of creating plug-ins for WordPress, so can’t answer that. All donations will be put to me buying books about WordPress, so you never know if I get enough donations, a WordPress plug-in could be next.

 


 

I may be able to answer some questions in the comments below, but am not a PHP or JavaScript guru.
 

Facebook Twitter Linkedin Digg Delicious Reddit Stumbleupon Tumblr Posterous Email Snailmail

Recent Tweets