Q. I’m writing an add-on or plugin that interacts with FeedWordPress, and I need to programmatically add a new subscription to the list of feeds that FeedWordPress syndicates content from. How do I do that?
A. The best way to do this is by using the FeedWordPress::syndicate_link
method. Let’s say that the URL for the feed you want to syndicate is in $newFeed
You’ll also need to provide a URL for the human-readable homepage associated with this feed (so, for example, for the feed http://feedwordpress.radgeek.com/feed/
the associated homepage URL is http://feedwordpress.radgeek.com/
), and the human-readable title associated with the feed (so, for example, for the feed http://feedwordpress.radgeek.com/feed/
, the associated title is “FeedWordPress”). Let’s say that these parameters are in the variables $newFeedPage
and $newFeedTitle
. (If you don’t yet know the right homepage URL or title for the feed, you can just supply dummy values for these variables — the title and the homepage URL will be read off of the feed you added the first time FeedWordPress checks it for posts.) You’d use the method like this:
Example:
$link_id = FeedWordPress::syndicate_link(
/*name=*/ $newFeedTitle,
/*homepage=*/ $newFeedPage,
/*feed=*/ $newFeed
);
The return value in $link_id
will either be a numeric ID > 0 (if FeedWordPress successfully added the new feed), or else a WP_Error object (if there was a problem adding the feed).
Note. This is a very bare-bones method; it adds the link to FeedWordPress’s subscription list and does nothing else with it after that. If you need to set some feed settings or do anything else with the link you’ve just added, then you should use the ID in the return value to create a new object of class SyndicatedLink and use that to carry on.
Example
// Add a new feed to the FWP subscription list
$link_id = FeedWordPress::syndicate_link(
/*name=*/ $newFeedTitle,
/*homepage=*/ $newFeedPage,
/*feed=*/ $newFeed
);
// Check to make sure that the link was successfully created
if (is_numeric($link_id) and $link_id > 0) :
$linkObj = new SyndicatedLink($link_id);
$linkObj->update_setting('fetch timeout', 600); // Set HTTP fetch timeout for new feed
// The link was not successfully created. Do something with the error
elseif (is_wp_error($link_id)) :
/* error handling... */
endif;