0
votes

I am using the FeedWordPress plugin http://wordpress.org/extend/plugins/feedwordpress/ to pull posts from one site to another.

I have written a filter that after some help from Stack users successfully scans the $content and extracts the image URL into $new_content

define('FWPASTOPC_AUTHOR_NAME', 'radgeek');

add_filter(
/*hook=*/ 'syndicated_item_content',
/*function=*/ 'fwp_add_source_to_content',
/*order=*/ 10,
/*arguments=*/ 2
);

function fwp_add_source_to_content ($content, $post) {
// Use SyndicatedPost::author() to get author
// data in a convenient array
$content = $post->content();

// Authored by someone else
if( preg_match( '/<img[^>]+src\s*=\s*["\']?([^"\' ]+)[^>]*>/', $content, $matches ) ) {
$new_content .= 'URL IS '.$matches[0].'';

return $new_content;
}
else
{
}
}

What I wanted to do now was save this URL into a custom field instead of just returning it. Has anyone achieved anything similar?

1

1 Answers

0
votes

So as I understand it, the plugin grabs content from external RSS feeds and creates them as posts in your website.

If this is the case, using your filter you should be able to grab the post ID within the $post variable.

So all you need is the add_post_meta() function to add a custom field to the specific post.

So including your code above it should look something like:

define('FWPASTOPC_AUTHOR_NAME', 'radgeek');

add_filter(
/*hook=*/ 'syndicated_item_content',
/*function=*/ 'fwp_add_source_to_content',
/*order=*/ 10,
/*arguments=*/ 2
);

function fwp_add_source_to_content ($content, $post) {
// Use SyndicatedPost::author() to get author
// data in a convenient array
$content = $post->content();

// Authored by someone else
if( preg_match( '/<img[^>]+src\s*=\s*["\']?([^"\' ]+)[^>]*>/', $content, $matches ) ) {
$new_content .= 'URL IS '.$matches[0].'';    

//Add custom field with author info to post
add_post_meta($post->ID, 'post_author', $new_content); 

return $new_content;
}
}