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:
- post: the post as an array.
- feed_source: the ID of the feed source, from which the post was imported.
- 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, 3 ); /** * 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 examples 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.
- Copy the code you need from above.
- Go to your WordPress site's dashboard.
- Go to Plugins > Add New.
- Search for Code Snippets, then install and activate the plugin.
- Once installed and activated, go to Snippets in your dashboard menu.
- Click on Add New.
- Add a Title, which could be the title of this article.
- Paste the code you copied in step 1 to the Code section.
- Add a Description or Tags if you wish to do so. It is not required.
- Click on Save Changes and Activate to save the filter and activate it.
- Or click on Save Changes to save the filter and activate it later.
- Your action or filter is now stored and active on your site.