Adding custom fields to your WordPress RSS feed

Published Categorised as Development Tagged

Our website uses the WP Job Manager plugin to manage the recruitment side of the web site, and after much hacking of the template, we’ve got it nailed now for the different job types in different job categories.

When asked to submit an RSS feed to AllTheTopBananas.com it came back as a fail as they need the salary and location fields. Hmmm, ok no problem. Add an action to put those custom fields into the RSS like this.

add_action('rss2_item', 'add_salary_location_to_job_rss');
function add_salary_location_to_job_rss() {
	// if the post_type is a job_listing...
	if (get_post_type()=='job_listing') {
		// add these custom fields with the XML namespace
		$fields = array( 
			'_job_salary'	=>	'salary', 
			'_job_location'	=>	'location',
		);
		$post_id = get_the_ID();
		foreach($fields as $field => $xml_key) {
			if ($value = get_post_meta($post_id, $field, true)) {
				echo "" . $value . "n";
			}
		}
	}
}

What could possibly go wrong?

Hmmmm guess who forgot how picky XML validation is and forgot to encode the $value.  Schoolboy error, so wrap the string with wptexturize():

echo "" . wptexturize($value) . "n";

And bingo. Validation finally. Maybe I should have checked with the W3C XML Validator first before sending it off again.

Leave a Reply