Hostingheaderbarlogoj
Join InMotion Hosting for $3.49/mo & get a year on Tuts+ FREE (worth $180). Start today.
Advertisement

How to Create an RSS Syndicator Site Using WordPress

by
Gift

Want a free year on Tuts+ (worth $180)? Start an InMotion Hosting plan for $3.49/mo.

In this tutorial, I will share on how to create a syndicator, or ping site, using WordPress functionality. This function can be useful for owners who want to create an article directory which will allow their users to submit articles to their website using RSS. To give some overview on how such a site may look, I've setup a Demo RSS Site for this tutorial. Now let's begin.


Step 1 Setting Up A Feed Placeholder

First we need to setup a place where users can add their RSS feeds. At first I thought using the existing WordPress user profile section would be sufficient, but if we want to allow multiple web submissions, it can be troublesome. We will setup a new custom post type to handle the URL for the user RSS Feed. You can learn further about custom post types by reading the Custom Post Type Helper Class article.

We will need 2 fields only for the custom post type, a title and a place to add the URL (I use the excerpt column to keep everything simple)

/* New custom post type for new RSS handling. */

add_action('init', 'create_rss_feed_uri');
function create_rss_feed_uri() {
	$labels = array(
		'name' => _x('RSS', 'All the RSS'),
		'singular_name' => _x('RSS', 'single item for rss'),
		'add_new' => _x('Add New RSS', 'rss'),
		'add_new_item' => __('Add New RSS'),
		'edit_item' => __('Edit RSS'),
		'new_item' => __('New RSS'),
		'view_item' => __('View RSS'),
		'search_items' => __('Search RSS'),
		'not_found' =>  __('Nothing found'),
		'not_found_in_trash' => __('Nothing found in Trash'),
		'parent_item_colon' => ''
	);
	
	$menu_args = array(
		'labels' => $labels,
		'label' => __('RSS'),
		'singular_label' => __('RSS'),
		'public' => true,
		'show_ui' => true,
		'capability_type' => 'post',
		'hierarchical' => false,
		'revisions' => true,
		'rewrite' => true,
		'supports' => array('title', 'excerpt')
	);
	register_post_type('rss',$menu_args);
}

Save the following code in new file (rss_feed_functions.php) and upload it into the following folder /wp-content/mu-plugins/. The function will be loaded without a need to activate it like a normal plugin.


Step 2 Setup Custom Page RSS Feed Submission

After we have setup our custom post type to keep the RSS URL, we need to setup a page where a user can submit their URL. The following function will create a form and you can include it into any page using the shortcode we define in the code.

We need an input field to allow a user to add a title and URL for the RSS.

<form action="" method="POST">
  <input type="text" value"" name="rss_title" />
  <input type="text" value"" name="rss_uri" />
  <input type="submit" value"Submit" name="submit" />
</form>

Next thing, we need to handle the form once it is posted. The following code will allow us to prepare the query to insert the data to our website.

/* prepare and process the rss data */
	$newrss_post = array(
		'post_title' => $_POST["rss_title"],
		'post_excerpt' => $_POST["rss_uri"],
		'post_author' => $current_user->ID,
		'post_status' => 'publish',
		'post_type' => "rss"
	);
	$post_id = wp_insert_post( $newrss_post );

But before we insert all the data we received from the form, we would want to validate the feed URL and the title first. To easily validate the feed URL, we will use the following WordPress function to check if the feed URL return a valid content.

include_once(ABSPATH . WPINC . '/feed.php');

		/*validate feed URL */
		$rss = fetch_feed(esc_url($_POST["rss_uri"]));

		if (!is_wp_error( $rss ) ) :
			/*prepare rss content */
			$newrss_post = array(
				'post_title' => esc_html($_POST["rss_title"]),
				'post_excerpt' => esc_url($_POST["rss_uri"]),
				'post_author' => $current_user->ID,
				'post_status' => 'publish',
				'post_type' => "rss"
			);

			/* insert rss details into database */
			$post_id = wp_insert_post( $newrss_post );
		else:
			echo "There was error validating your RSS Feed, please try again";
		endif;

If everything works well, we insert the feed data into the database. The complete function with shortcode functionality will look like the code shown below, we can add the complete function into our rss_feed_functions.php file.

add_shortcode( 'rss_form_submit', 'create_rss_feed_form' );
/* Form to submit new RSS URL */
function create_rss_feed_form(){
	echo '<form action="" method="POST">
			<input type="text" value""="" name="rss_title">
			<input type="text" value""="" name="rss_uri">
			<input type="submit" value"submit"="" name="submit">
			</form>';

	if(isset($_POST["rss_title"])):

		/*validate feed URL */
		$rss = fetch_feed(esc_url($_POST["rss_uri"]));

		if (!is_wp_error( $rss ) ) :
			/*prepare rss content */
			$newrss_post = array(
				'post_title' => esc_html($_POST["rss_title"]),
				'post_excerpt' => esc_url($_POST["rss_uri"]),
				'post_author' => $current_user->ID,
				'post_status' => 'publish',
				'post_type' => "rss"
			);

			/* insert rss details into database */
			$post_id = wp_insert_post( $newrss_post );
			echo "RSS successfully submitted";
		else:
			echo "There was error validating your RSS Feed, please try again";
		endif;

	endif;
}

Now we can use the shortcode [rss_form_submit] in any of our pages and you can style the form to fit your website design. You may want to allow only registered user to be able to see and use that page.


Step 3 Article Submission via Feed URL

Since we already have the feed URL in our database, next we want to process the article that users want to submit to our ping site. We will need a new page for this, and to ensure the function can be used on any themes, we will use the same approach as the feed URL submission for the article submission, using a function with shortcode.

First of all, we need to read the feed from the current user.

	global $current_user, $wpdb;
	get_currentuserinfo();

	/*custom query to retrieve custom post type without rendering it */
	$rssfeed = $wpdb->get_results(" SELECT *
									FROM $wpdb->posts
									WHERE post_type = 'rss'
									AND post_status = 'publish'
									AND post_author  = ".$current_user->ID."");

If there is RSS feed found, then we can start loop the feed URL, and fetch the latest article from the feed.

foreach ( $rssfeed as $post ):
	setup_postdata( $post );
	echo "<h3>".$post->post_title."</h3>";

	$rss = fetch_feed(esc_url($post->post_excerpt));

	if (!is_wp_error( $rss ) ) :
		$item = $rss->get_item(0); 
		echo esc_html($item->get_title());
	endif;

endforeach;

Now we already have the first article to be inserted into our website. Before we continue with adding it into the database, it is best to check first if the article already exists on our website. We will check the title of the feed item if it already exists, this is just a basic method, you can check more attributes to ensure there are no duplicated article in the database.

	if($wpdb->get_row("SELECT post_title FROM $wpdb->posts WHERE post_title = '" . $item -> get_title(). "' AND post_status = 'publish' ", 'ARRAY_A')):
		echo "No new article found";
	else:
		echo esc_html($item->get_title());
	endif;

We can now process the data we fetch from the feed into our database. To make it simple for our user, we will include the submit form directly after we fetch and check the URL.

	echo '<form action="" method="POST">
			<strong>'.esc_html($item->get_title()).'</strong>
			<input type="hidden" value="'.esc_html($item->get_title()).'" name="post_title">
			<input type="hidden" value="'.esc_html($item->get_content()).'" name="content">
			<input type="hidden" value="'.esc_url($item->get_permalink()).'" name="permalink">
			<input type="hidden" value="'.$item->get_date('Y-m-d H:i:s').'" name="date">
			<input type="submit" value="Submit" name="submit">
		</form>';

In the example, we process the title, content, permalinks and date to be inserted into our database. Users will see the title and the submit button like the screenshot below.

Last step we need to do, is to insert the data into the database. We prepare the statement before we run the query.

	$prepare_content = array(
		'post_date' => $_POST["date"],
		'post_title' => esc_html($_POST["post_title"]),
		'post_content' => $_POST["content"],
		'post_author' => $current_user->ID,
		'post_status' => 'publish',
		'post_type' => 'rss'
	);

We will process these details first before we insert the extra data into our custom meta box.

	$post_id = wp_insert_post( $prepare_content );
	add_post_meta($post_id, 'feed_item_permalink', esc_url($_POST["permalink"]));

Now all the article data is successfully inserted into the database. We now have a syndicator or ping site ready to be use. There are more possibilities you can add to enhance the quality of your ping site, but this was a simple introduction.


Conclusion

I have included the complete source code ( link at the top of this article ) that I use on the Demo RSS Site. If you have any suggestion or improvement for this tutorial, I'm looking forward to reading the ideas and suggestions in the comments below.

Advertisement