WordPress RSS import class to custom fields (3 posts)

  1. Austin Passy
    Posted 6 years ago #

    I've taken much of the code from the wordpress-import.php file so I know I am on the right path. But.. I'm stuck trying to import my Twitter RSS feed and the <twitter:place>..</> fields into custom fields.

    For the latest updated code see: http://snipt.org/Zkkl.

    Right now this is what I've got (besides the whole rest of the code which works just fine). I am just trying too proccess the extra info from the feed into a custom field(s).

    $postmeta = array();
    $postmeta[] = preg_match('|<twitter:id>(.*?)</twitter:id>|is', $post, $postmeta);
    $postmeta[] = preg_match('|<twitter:name>(.*?)</twitter:name>|is', $post, $postmeta);
    $postmeta[] = preg_match('|<twitter:full_name>(.*?)</twitter:full_name>|is', $post, $postmeta);
    $postmeta[] = preg_match('|<twitter:place_type>(.*?)</twitter:place_type>|is', $post, $postmeta);
    $postmeta[] = preg_match('|<twitter:url>(.*?)</twitter:url>|is', $post, $postmeta);
    //$postmeta = $postmeta[1];
    if ( $postmeta) { foreach ($postmeta as $p) {
    	$keys = preg_match('|<twitter:(.*?)>|is', $post, $postmeta);
    		foreach($keys as $key) {
    			$key = $this->get_tag( $p, 'twitter:{$k}' ); //how do I get the value after the colon?
    		foreach($values as $value) {
    			$value = $this->get_tag( $p, 'twitter:' );
    	$this->process_post_meta($post_id, $key, $value);
    } }		
    // Process the custom meta
    function process_post_meta($post_id, $key, $value) {
    	// the filter can return false to skip a particular metadata key
    	$_key = apply_filters('import_post_meta_key', $key);
    	if ( $_key ) {
    		add_post_meta( $post_id, $_key, $value );
    		do_action('import_post_meta', $post_id, $_key, $value);
    // Get tag function
    function get_tag( $string, $tag ) {
    	global $wpdb;
    	preg_match("|<$tag.*?>(.*?)</$tag>|is", $string, $return);
    	if ( isset($return[1]) ) {
    		$return = preg_replace('|^<!\[CDATA\[(.*)\]\]>$|s', '$1', $return[1]);
    		$return = $wpdb->escape( trim( $return ) );
    	} else {
    		$return = '';
    	return $return;
  2. Austin Passy
    Posted 6 years ago #

    Wondering if I just have to write out each source?

    preg_match_all('|<twitter:source>(.*?)</twitter:source>|is', $post, $postmeta);
    			$postmeta = $postmeta[1];
    			if ($postmeta) { foreach ($postmeta as $p) {
    				$key = 'twitter_source';
    				$value = $this->get_tag( $p, 'twitter:source' );
    			} }
  3. Austin Passy
    Posted 6 years ago #

    Someone suggested a better regex so I thought this would work better as a multiple matcher.

    preg_match_all('|<twitter:(?<key>\w+)>(?<value>.*?)</twitter:\w+>|is', $post, $postmeta);
        $postmeta = $postmeta[1];
    	if ($postmeta) { foreach ($postmeta as $p) {
    		$key = 'twitter_<key>';
    		$value = $this->get_tag( $p, 'twitter:<key>' );
        } }

    Thought I don't think that's the correct way to write out the key..

Topic Closed

This topic has been closed to new replies.

About this Topic