WordPress.org

Ready to get started?Download WordPress

Forums

Problem using the 'rss2_item' action hook (3 posts)

  1. David Gard
    Member
    Posted 2 years ago #

    Hey all,

    I'm trying to add some custom data to the feeds that are being produced by my website, but the data is not being added. I'm using the code below, which seems to be doing something, as my other site which is reading the feed tells me there are no results, but is fine when this code is not used.

    I've never delved in to the world of RSS manipulation before, so I'm hoping that this is a fairly simple problem!!

    /**
     * Adds the 'event_date' meta value to a feed
     */
    add_action('atom_entry', 'add_event_date_to_feed');
    add_action('rdf_item', 'add_event_date_to_feed');
    add_action('rss_item', 'add_event_date_to_feed');
    add_action('rss2_item', 'add_event_date_to_feed');
    function add_event_date_to_feed(){
    
    	global $post;
    
    	$event_date_raw = get_post_meta($post->ID, 'event_date', true);
    	if($event_date_raw && $event_date_raw !== '') :
    		$date_object = DateTime::createFromFormat('D, d M Y H:i:s +0000', $event_date_raw);
    		$event_date = $date->format('U');
    	else :
    		$event_date = '';
    	endif;	printf("\t\t".'<eventDate>%1$s</eventDate>'."\n", $event_date);
    
    }

    Thanks.

  2. mehulved
    Member
    Posted 1 year ago #

    Are you getting the $post->ID? I am unable to get that and thus stuck with a similar problem.

  3. David Gard
    Member
    Posted 1 year ago #

    I'll be honest, I can't remember exactly what was going on with the above as it was so long ago, but I think it was to do with a probelme with how I was creating the date to add to the feed.

    Here is the code that I use (and that works) on my site now.

    The global $post; call allows me to get $post->ID just fine.

    /**
     * Adds the 'event_date' meta value to a feed
     */
    add_action('atom_entry', 'add_event_date_to_feed');
    add_action('rdf_item', 'add_event_date_to_feed');
    add_action('rss_item', 'add_event_date_to_feed');
    add_action('rss2_item', 'add_event_date_to_feed');
    function add_event_date_to_feed(){
    
    	global $post;
    
    	$event_date_raw = get_post_meta($post->ID, 'event_date', true);
    	if($event_date_raw && $event_date_raw !== '') :
    		$date_object = DateTime::createFromFormat('d/m/Y', $event_date_raw);
    		$event_date = $date_object->format('D, d M Y 23:59:59 O');
    	else :
    		$event_date = '';
    	endif;
    
    	printf("\t\t".'<eventDate>%1$s</eventDate>'."\n", $event_date);
    
    }

    If you are still having problems, post some code and I'll try to help.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.