Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

*Editor Note (Original title: The url from an image via custom field) changed to be more descriptive.

Hi all, Am not even sure if this can be done but...after ive been searching for the last 6 hours none stop I have to ask :/

I have a image slider I use which I have to add the image url(not the thumb url) all the time in a custom field (postimage) I have set up a rss feed going in to wordpress from my booru animelon.com/booru It works fine but I need a way to get it to auto add the image url in this custom field

share|improve this question
add comment

2 Answers

This is a hard one but I think this will work. Someone please correct me if I'm wrong.

This uses the external Simple Pie library built into WordPress to fetch the feed, get the image url and create a new post for each item and save the image url as a custom field.

To activate the process we have to hook into wp_cron. The code below does it daily but it would probably be better to do it weekly to prevent overlap. Some overlap will probably occur so this still needs a way to check if we have already imported the image

First we need a function to save the custom field after the post has been created. This section comes from another answer I found on WordPress Answers.

Edit:

This needs to be wrapped in a plugin to schedule the cron event and the cron event was missing the action to make it fire.

Edit: #2:

I Tested the plugin and made a few edits and it fully works except that the RSS feed we are fetching contains relative urls so in the code the slider code you will need to echo out the domain name then the custom field: ie:

'<img src="http://animelon.com<?php get_post_meta($post->ID, 'feed_image_url', true); ?>" />

<?php
/*
Plugin Name: Fetch The Feed Image
Version: 0.1
Plugin URI: http://c3mdigital.com       
Description: Sample plugin code to fetch feed image from rss and save it in a post
Author: Chris Olbekson
Author URI: http://c3mdigital.com
License: Unlicense For more information, please refer to <http://unlicense.org/>
*/

//Register the cron event on plugin activation and remove it on deactivation

register_activation_hook(__FILE__, 'c3m_activation_hook');
register_deactivation_hook(__FILE__, 'c3m_deactivation_hook');

add_action( 'c3m_scheduled_event', 'create_rss_feed_image_post');
function c3m_activation_hook() {
    wp_schedule_event(time(), 'weekly', 'c3m_scheduled_event');
}

function c3m_deactivation_hook() {
 wp_clear_scheduled_hook('c3m_scheduled_event');    
}


function create_rss_feed_image_post() {
     if(function_exists('fetch_feed')) {
            include_once(ABSPATH . WPINC . '/feed.php');               // include the required file
            $feed = fetch_feed('http://animelon.com/booru/rss/images'); // specify the source feed

    }       

        foreach ($feed->get_items() as $item) :

        //  global $user_ID;
            $new_post = array(
            'post_title' =>  $item->get_title(),
            'post_status' => 'published',
            'post_date' => date('Y-m-d H:i:s'),
            //'post_author' => $user_ID,
            'post_type' => 'post',
            'post_category' => array(0)
            );
            $post_id = wp_insert_post($new_post);

        if ($enclosure = $item->get_enclosure() )

            update_post_meta( $post_id, 'feed_image_url', $enclosure->get_link() );
        endforeach;
    }

Screenshot of the new posts pulled in:

enter image description here

Screenshot of the custom field created:

enter image description here

share|improve this answer
add comment

add a filter to the_content that checks if it's a feed and adds the image.

add_filter('the_content', 'add_image_to_feed');

function add_image_to_feed( $content ){
if( is_feed() ){
    $image = get_post_meta($post->ID, 'Your Custom Field');
    $content = '<img src="'.$image.'" />'.$content;
    return $content;
} else {
    return $content;
}
}

at least I think that'll work :)

share|improve this answer
    
Thanks for the reply Not sure I understand this.. Is this to add a image to the feed? if so am doing it the other way my feed is coming into wordpress and posting (in wordpress) from animelon.com/booru/rss/images Thanks –  VKJSM Apr 26 '11 at 23:17
    
oops, misunderstood, thought you wanted to add a custom field to the feed. disregard. –  Milo Apr 26 '11 at 23:33
    
Thanks anyway Milo :) –  VKJSM Apr 26 '11 at 23:37
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.