0
votes

WordPress RSS feed not work when custom filed have '&'. i think that may be XML error with character '&'. How could I change to that & to '&amp' in custom field in wp-php code or any working way? Error !! I am using following code

function customFieldFilter($id) {
$meta = get_post_meta($id);
foreach ($meta as $key => $value) {
   update_post_meta($id, $key, esc_attr($value));
}

} add_action('pre_post_update', 'customFieldFilter');

1
Your question needs significantly more detail. Please tell us exactly what you're trying to do; paste any code you're using and show us a screenshot or paste the error you're receiving. - turbonerd
In simple Question is : when custom field value contain character '&' entire RSS feed not working. when I add &amp instant of &. That is working. Is there any wp-function to character encode in custom fields? or any other way to solve this? - Miuranga

1 Answers

1
votes

When you're saving your custom field, try wrapping it in the esc_html WordPress function.

I'm not sure exactly how you save your custom fields, as you haven't provided any code, but for me this would be something along the lines of:

$habitat = esc_html( $_POST["habitat"] );
update_post_meta( $post->ID, "habitat", $_POST["habitat"] );

However, I would really like to see the way you're saving your custom fields as I believe that esc_html should be run by the update_post_meta function?


EDIT

Try adding this to your theme's functions.php (right at the bottom):

function custom_field_filter( $id ) {
    $meta = get_post_custom( $id );
    foreach ( $meta as $key => $value )
        update_post_meta( $id, $key, esc_html( $value ) );
}

add_action('pre_post_update', 'custom_field_filter', 100);

This is pure guess-work I'm afraid, I really can't find a lot of information on custom fields. This should retrieve all of the custom fields attached to the post, on save, and run them through a function to encode the & character to & which will hopefully fix your RSS feed.