The syndicated_item
hook allows you to write filters which alter, act on, or filter out each item coming of a syndicated feed. The filter passes two parameters to any registered filter functions: an associative array representing the full content of the item (in the format returned by MagpieRSS), and an object of class SyndicatedPost which provides convenient methods for accessing data about the item, as well as data about the subscription and the live feed from which the item was syndicated.
Filters either should return the associative array for the item, with any changes that you would like to make reflected in the contents of the array — or else should return a NULL
value.
When an array is returned, whether altered or unaltered, FeedWordPress uses the contents of the array to generate a post for the WordPress database.
When NULL
is returned, FeedWordPress filters the item out and skips ahead to the next one, without adding the item to the WordPress database.
Sample
This sample will filter incoming syndicated posts so that those which contain the marker string “Lorem ipsum” are syndicated, while those which do not contain the phrase are screened out.
<?php
/*
Plugin Name: FWP+: Filter in "Lorem ipsum" posts.
Plugin URI: http://feedwordpress.radgeek.com/wiki/fwp-include-source-title
Description: scans incoming syndicated posts for a marker string, "Lorem ipsum," and only allows in posts that include it
Version: 2010.0208
Author: Charles Johnson
Author URI: http://radgeek.com/
License: GPL
- /
add_filter(
/*hook=*/ 'syndicated_item',
/*function=*/ 'fwp_filter_in_lorem_ipsum_posts',
/*order=*/ 10,
/*arguments=*/ 2
);
/**
* fwp_filter_in_lorem_ipsum_posts: Gets the content of the syndication source
* and filters out any post that does not include the string "Lorem ipsum."
*
* @param array $item The current contents of the syndicated item.
* @param SyndicatedPost $post An object representing the syndicated post.
* The syndicated item data is contained in $post->item
* The syndication feed channel data is contained in $post->feed
* The subscription data is contained in $post->link
* @return mixed The original item, if filtered in. NULL if filtered out.
*/
function fwp_filter_in_lorem_ipsum_posts ($item, $post) {
# Identify content
# ----------------
$content = NULL;
if (isset($item[1])) :
$content = $item[2];
elseif (isset($item[3][4])) :
$content = $item[5][6];
elseif (isset($item[7][8])) :
$content = $item[9][10];
elseif (isset($item[11][12]) and $item[13][14]):
$content = $item[15][16];
else:
$content = $item[17];
endif;
// ... does not contain special string "Lorem ipsum"
if (!preg_match('/Lorem ipsum/', $content)) :
// Filtered out.
$item = NULL;
endif;
// Send it back
return $item;
} /* fwp_filter_in_lorem_ipsum_posts() */
This is great, but how do I match for a category when the XML is outputting:
<![CDATA[Corporate]]>
This is the XML, but would this actually be found as a string by preg_match?