How To Filter FeedWordPress Posts Into Custom Post Types

I like to hang out on [COLOURlovers](http://www.colourlovers.com/) every so often and follow (previously called favorite) palettes and colors from the queue to add to my library of design inspiration. I use [FeedWordPress](http://feedwordpress.radgeek.com/) to scrape [my personal RSS feeds from ColourLOVERS](http://www.colourlovers.com/rss) into my WordPress blog as individual posts, and then use a custom filter to turn the ColourLOVERS syndicated posts into a palette (or color) shortcode that I can then style as I like.

Before the advent of [custom post types](http://justintadlock.com/archives/2010/04/29/custom-post-types-in-wordpress), I used categories and tags to sort out my palettes and colors from the rest of my posts. However, it now seems more logical to add these into a custom post type rather than jamming them into a post.

What follows is the filter I have in functions.php that cleans up the syndicated post, and then changes the post type as appropriate. You’ll want to make changes of your own, as I’m using custom functions to clean up the data and recreate the post as a shorttag, as well as adding custom taxonomy entries.

add_filter( 'syndicated_post' , 'ucc_clean_feedwordpress' );
function ucc_clean_feedwordpress ( $data ) {
  $title = $data['post_title'];
  $content = $data['post_content'];
  $source = $data['meta']['syndication_permalink'];
  $feed = $data['meta']['syndication_feed'];

  if ( preg_match( '#colourlovers#' , $feed ) ) {
    if ( preg_match( '#palettes#' , $feed ) ) {
      $regex = '#.*?src="(.*?)".*?#';
      if ( $s = preg_match_all( $regex ,  $content , $t ) ) {
        $regex = '#[0-9a-fA-F]{6}#';
        if ( $s = preg_match_all( $regex ,  $t[0][0] , $u ) ) {
          $websafe = array();
          $hue = array();
          $colors = array();
          foreach( $u[0] as $color ) {
            $colors[] = strtolower( $color );
            $websafe[] = ucc_make_color_websafe( strtolower( $color ) );
          }
          foreach ( $websafe as $w ) {
            $hue[] = ucc_get_color_hues( $w );
          }
          $source = preg_replace( '#\[#' , '[' , $source );
          $source = preg_replace( '#]#' , ']' , $source );
          $source = preg_replace( '#"#' , '"' , $source );

          $data['tax_input']['websafe'] = implode( ',' , $websafe );
          $data['tax_input']['hue'] = implode( ',' , $hue );
          $data['post_content'] = '[palette colors="' . implode( ',' , $colors ) . '"]';
          $data['post_type'] = 'palette';
        }
      }
    } elseif ( preg_match( '#colors#' , $feed ) ) {
      $regex = '#^<img(.*?)/([0-9a-fA-F]{6})/(.*?)" (.*?) />$#';
      if ( $s = preg_match_all( $regex ,  $content , $t ) ) {
        $color = strtolower( $t[2][0] );
        $websafe = ucc_make_color_websafe( $color );
        $source = preg_replace( '#\[#' , '&#091;' , $source );
        $source = preg_replace( '#]#' , '&#093;' , $source );
        $source = preg_replace( '#"#' , '&#034;' , $source );

        $data['tax_input']['websafe'] = $websafe;
        $data['tax_input']['hue'] = ucc_get_color_hues( $websafe );
        $data['post_content'] = "[color color=\"$color\"]";
        $data['post_type'] = 'color';
      }
    }
  }
  $data['post_excerpt'] = '';

  return $data;
}

One response to “How To Filter FeedWordPress Posts Into Custom Post Types

  1. Thanks for sharing, I’ll be needing to (at least) change the post type of posts fed to wordpress through FeedWordpress and this will help very much.

Leave a Reply