How to create a javascript widget to load recent posts from my wordpress site and display in other sites?

I have a WordPress site. I want to allow others to place a web widget in their website which grabs recent posts from my site and display title with a link to its page for each of my recent posts. I know that this is possible through using iframe. But this has no positive effect on my website SEO. I found a solution here to use JavaScript for similar purpose. But it used Python and JSON to load HTML from the original site in the main() function. I am not familiar with JSON and Python. Is there any solution that I can return my recent posts using PHP and put it in the main function?

Related posts

Leave a Reply

1 comment

  1. I use SimplePie to get RSS feeds. This is the function I use.

    function start_rss(){
    $urls = array(
        0 => 'http://ogmarketreport.com',
        1 => 'http://petroriveroil.com/feed/'
    );
    $numberOfPosts = 10;
    
    foreach ($urls as $key => $value) {
        $url = $value;
        $feed = new SimplePie();
        $feed->set_cache_location($_SERVER['DOCUMENT_ROOT'] . '/cache/');
        $feed->set_feed_url($url);
        $feed->set_stupidly_fast(true);
        $feed->init();
        $feed->handle_content_type();
        $blog_title =  $feed->get_title();
        $blog_title_url = '<div class="row" style="width: 100%; display: block;"><h2><a href="<?php $feed->get_permalink(); ?>" target="_blank"><?php $title; ?></a></h2></div>';
        $blog_desc = $feed->get_description();
        $html = "";
        $x = $feed->get_items(0, $numberOfPosts);
    
        $html .= '<div class="container" style="width: 100%; display: block;"><h2><a target="_blank" href="' .$feed->get_permalink() . '">' . $feed->get_title() . '</a></h2></div>';
    
        if (count($x) == 0 ) {
            $html .= '<article class="rss col-md-12">'; 
            $html .= '<p class="danger">No Recent Posts.</p>';  
            $html .= '</article>';
        } else {
            foreach ($x as $item) {
                $html .= '<article class="rss col-md-12">'; 
                $html .= '<h4 class="title">';
                $html .= '<a href="' .$item->get_permalink() . '">' .$item->get_title() . '</a>' ;
                $html .= '</h4>';
                $html .= '<p>';
                $html .= $item->get_description();
                $html .= '</p>';
                $html .= '<p><small>';  
                $html .= "Posted on ";
                $html .= $item->get_date('j F Y | g:i a');
                $html .= '</small></p>';
                $html .= '</article>';  
            }
    
        }
        print_r($html);
    }