octavian-filipciuc
7/24/2014 - 7:25 PM

Facebook Feed

Facebook Feed

Using: <?php echo do_shortcode('[get_latest_facebook_posts limit="2"]'); ?>
/*
  Plugin Name: Get Latest Facebook Posts
  Description: Adds a shortcode tag [get_latest_facebook_posts limit="number-of-facebook-posts"] to display recent facebook posts
  Version: 0.1
  Author: Filipciuc Octavian
 */

function get_latest_facebook_html($attributes) {
    $data = extract(shortcode_atts(array(
                'limit' => null
                    ), $attributes));

    $limit = intval($limit);
    if ($limit < 1 or $limit > 100) {
        return "Numbers of tweets must be between 1 and 100.";
    }
    ini_set("user_agent","443938580493");
    $doc = new DOMDocument();
    $feed_burner_url = 'http://www.facebook.com/feeds/page.php?format=atom10&id=443938580493';
    $doc->load($feed_burner_url);

    $feeds = array();
    $counter = 1;


    foreach ($doc->getElementsByTagName('entry') as $node) {

        if ($counter <= $limit) {
            $br_counterss = $node->getElementsByTagName('content')->item(0)->nodeValue;
            $br_countersss = preg_replace("/<img[^>]+\>/i", "", $br_counterss);
            $br_counters = preg_replace('/<a[^>]+\>/i', '', $br_countersss);
            $title_counters = $node->getElementsByTagName('title')->item(0)->nodeValue;

            $items = array(
                'title' => $node->getElementsByTagName('name')->item(0)->nodeValue,
                'description' => $br_counters,
                'pubDate' => $node->getElementsByTagName('published')->item(0)->nodeValue
            );

            array_push($feeds, $items);
        }
        $counter++;
    }

    foreach ($feeds as $feed) {
        if (strlen($feed['description']) > 154) {
            $dots = "...";
        } else
            $dots = "";
        $last_contentt = str_replace('<br/>', ' ', $feed['description']);
        $last_contentt = strip_tags($last_contentt);
        $last_content = substr($last_contentt, 0, 155);
        $date_replace = array("+01:00", "T");
        $feed_pub_date = str_replace($date_replace, ' ', $feed['pubDate']);

        if (function_exists("time_ago")) {
            $fb_date_pub = time_ago($feed_pub_date);
        } else
            $fb_date_pub = $feed_pub_date;

        $content = '<li class="facebook_post_data"><span class="title_facebook_post">' . ucwords(strtolower($feed['title'])) . '</span><div class="facebook_desc">' . $last_content . $dots . '</div><span class="facebook_post_date">' . $fb_date_pub . '</span></li>';
        echo $content;
    }
}

add_shortcode('get_latest_facebook_posts', 'get_latest_facebook_html');