Wordpress plugin shortcode to insert news articles using API

Following Wordpress plugin shortcode is used to search and insert news details from guardian.com. This template is deisgned based on guardian.com job search api.

Please signup for api key before using this template.
<?php /* Plugin Name: insertnews Description: Import guardian news Version: 0.1 Author: http://forum.indiacitys.com */ function insertnewsguardian() { $string = file_get_contents('http://content.guardianapis.com/search?page-size=50&format=xml&show-fields=all&show-tags=all&api-key=xxxxxxxxxxxxxxxx'); $xml = simplexml_load_string($string); //Loop trough multiple products foreach($xml->results->content as $content) { foreach($content[0]->attributes() as $fieldnameurl => $fieldurlcontent) { if ($fieldnameurl == "id") { $wordpress_id = $fieldurlcontent; } } $wordpress_title = ''; $wordpress_body = ''; $wordpress_thumbnail = ''; $wordpress_standfirst = ''; $wordpress_post_keywords = 'featured-post'; foreach($content->fields->field as $field) { $fieldname = $field[0]->attributes(); if ($fieldname == "headline") { $wordpress_title = $field[0]; } if ($fieldname == "body") { $wordpress_body = $field[0]; } } $tagkeyword=''; foreach($content->tags->tag as $tag) { $tagname = $tag->attributes(); if ($tagname["type"] == "keyword") { $tagkeyword = $tagkeyword.",".$tagname["web-title"]; } } if ($wordpress_body != '<!-- Redistribution rights for this field are unavailable -->') { $wordpress_body = "[insertnews nid='".$wordpress_id."']"; $wordpress_post_content = $wordpress_body; $post_id = wp_insert_post( array( 'post_title' => $wordpress_title, 'post_content' => $wordpress_post_content, 'post_category' => array('94968'), 'post_type' => 'post', 'tags_input' => $tagkeyword, 'post_status' => 'draft', )); } } } register_activation_hook( __FILE__, 'insertnewsguardian' ); ?>
Sign In or Register to comment.