F2P Filter: Modify Imported Posts Before They Are Saved

The filter below allows you to modify imported posts before they are saved and inserted into the database.

The filter parameters are:

  1. post: the post as an array.
  2. feed_source: the ID of the feed source, from which the post was imported.
  3. item: the instance of SimplePie_Item that represents the feed item being imported.

The post array returned by the filter will be passed to the WordPress  wp_insert_post function. Refer to this function’s documentation for a full list of post properties available in the post parameter.

add_filter( 'wprss_ftp_post_args', 'my_custom_post_args', 10, 2 );
/**
* Some examples.
*/
function my_custom_post_args( $post, $feed_source, $item ) {
// Copy the post. Recommended
$new_post = $post;


// Add new properties, for instance an excerpt
$new_post['post_excerpt'] = '...';


// Remove properties
unset( $new_post['post_excerpt'] );


// Change some properties, depending on the feed source
if ( $feed_source === 12 ) {
// Add "IMPORTANT!" to the post titles for posts imported
// from the feed source with ID 12
$new_post['post_title'] = 'IMPORTANT! ' . $new_post['post_title'];
}


// Return the new post
return $new_post;
}

Some other example of how to use this filter would be to add the text “IMPORTED” to the posts imported from two feed sources with IDs 11 and 14. The code snippet would look like this:

add_filter( 'wprss_ftp_post_args', 'my_custom_post_args', 10, 2 );
 
function my_custom_post_args( $post, $feed_source ) {
 
// Copy the post. Recommended.
$new_post = $post;
 
// Change some properties, depending on the feed source.
if ( in_array($feed_source, array(11, 14)) ) {
 
// Add "IMPORTED" to the post titles for posts imported
// from the feed sources with IDs 11 and 14
$new_post['post_title'] = 'IMPORTED ' . $new_post['post_title'];
}
 
// Return the new post
return $new_post;
}

How do I add this to my site?

Follow the step-by-step instructions below to add this filter to your WordPress site.

  1. Copy the code you need from above.
  2. Go to your WordPress site's dashboard.
  3. Go to Plugins > Add New.
  4. Search for Code Snippets, then install and activate the plugin.
  5. Once installed and activated, go to Snippets in your dashboard menu.
  6. Click on Add New.
  7. Add a Title, which could be the title of this article.
  8. Paste the code you copied in step 1 to the Code section.
  9. Add a Description or Tags if you wish to do so. It is not required.
  10. Click on Save Changes and Activate to save the filter and activate it.
    1. Or click on Save Changes to save the filter and activate it later.
  11. Your action or filter is now stored and active on your site.

Still need help? Contact Us Contact Us