Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm trying to get contents from a blog of mine. Suppose it's example.com.

I've used the php script below in my non-WordPress website trying to get contents from a WordPress blog.

$content = file_get_contents('http://blog.example.com/feed/');
$clean = str_replace( ']]>', '', str_replace('<![CDATA[', '', $content) );

$feed = simplexml_load_string( $clean );

for($i = 0; $i < 4; $i++) {                   
  echo '<div style="width: 100%;float:left;margin-bottom:30px; font-size: 16px;">';
  echo '<div style="width:100%;float:left;margin-bottom:30px; font-weight:bold;">';
  echo '<a href="' . $feed->channel->item[$i]->link . '" style="color:#f6a236;">';
  echo $feed->channel->item[$i]->title . '</a></div>'; 
  echo '<span style="color:#444645;">'.$feed->channel->item[$i]->description.'</span>';
  echo '</div>';
}

However, this code doesn't work. Someone can say me why and how is possible to output WordPress feed in a non-WordPress site?

share|improve this question
    
Please explain in which context this code is called. –  kaiser Mar 5 at 12:53
    
This code is getting contents using file_get_contents('blog.example.com/feed/'); Even if my question is wrong, please edit and you can make it perfect. As you can see I've just started using this site. –  ST'' Mar 5 at 12:59
    
Are you trying to scrape a feed and do bulk imports or something? Are you doing this from within a custom plugin? Does this run on default cron or WP Cron? Please file edits to add further detail. If you are not sure how to write a question that attracts the most answers, please see How to Ask. –  kaiser Mar 5 at 13:11
    
I'm trying to get feed from a wordpress blog to my another website(which is not in wordpress), Sir. I'm not using any plugins. –  ST'' Mar 5 at 13:21
    
You misunderstood me: Is above code in a plugin? A file that contains a PHPDocBlock like /** Plugin Name: or called from a file that has this doc block? Or do you call it from a themes functions.php file? –  kaiser Mar 5 at 13:29

1 Answer 1

up vote 2 down vote accepted

The simplest way to use WordPress feed from non-WordPress sites is relying on a library: SimplePie that is the same used by WordPress itself.

You can download it from its site, there is also a minified version.

Normally you download it as zip package, extract it in a folder, name it simplepie, and put in your non-WordPress in the same folder of the file that should output the feed.

After that using it is very easy, in the file where you want to output feed just do something like:

<?php
// following lines go before open html tag
require_once 'simplepie/autoloader.php';
$feed = new SimplePie();
$feed->set_feed_url( 'http://blog.example.com/feed/' );
$success = $feed->init();
$feed->handle_content_type();
?>

<html lang="en-US">
<head>
<title>SimplePie Demo</title>
<link rel="stylesheet" href="yourstylesheet.css" type="text/css" media="screen">
</head>
<body>

<?php
if ($success) {
  foreach( $feed->get_items() as $item ) {

    echo '<h4><a href="' . $item->get_permalink() . '">';
    echo $item->get_title() .'</a></h4>';
    echo '<p>' . $item->get_date('j M Y, g:i a') . '</p>';
    echo '<p>' . $item->get_content() . '</p>';

  }
} else {
   echo 'Error on parsing feed';
}
?>

</body>
</html>

Of course feel free to customize the markup, the mine is just a quick rough example.

Consider that SimplePie has powerful functions to handle categories and tags, images and other media, cache, sanitization and much more.

See documentation and API docs and also have a look at the demo folder in SimplePie package, it contains useful examples.

share|improve this answer
    
it says "Error on parsing feed" –  ST'' Mar 5 at 14:37
    
Probably your WordPress blog outputs a bad formatted feed. Check it @ST'' –  G. M. Mar 5 at 14:44

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.