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.

I'm getting the following PHP warnings when using the WordPress plug-in "Feed JSON". Can someone tell me how to fix them?

Warning: Invalid argument supplied for foreach() in /mypath/wp-content/plugins/feed-json/feed-json-template.php on line 39

Warning: Cannot modify header information - headers already sent by (output started at /mypath/wp-content/plugins/feed-json/feed-json-template.php:39) in /mypath/wp-content/plugins/feed-json/feed-json-template.php on line 53

Here is the code used in feed-json-template. Lines 39 and 53 are highlighted with ** around each line.

<?php
/**
 * JSON Feed Template for displaying JSON Posts feed.
 *
 */
$callback = trim(esc_html(get_query_var('callback')));
$charset = get_bloginfo('charset');

if ( have_posts() ) {
    $json = array();
    while ( have_posts() ) {
        the_post();
        $id = (int) $post->ID;

        $single = array(
            'id'        => $id ,
            'title'     => get_the_title() ,
            'permalink' => get_permalink(),
            'content'   => get_the_content(),
            'excerpt'   => get_the_excerpt(),
            'date'      => the_date('Y-m-d H:i:s','','',false) ,
            'author'    => get_the_author() ,
            );

        // thumbnail
        if (has_post_thumbnail($id)) {
            $single["thumbnail"] = preg_replace("/^.*['\"](https?:\/\/[^'\"]*)['\"].*/i","$1",get_the_post_thumbnail($id));
        }

        // category
        $categories = array();
        foreach((get_the_category()) as $category) { 
            $categories[] = $category->cat_name; 
        }
        $single["categories"] = $categories;

        // tag
        $tags = array();
        **foreach((get_the_tags()) as $tag) {** 
            $tags[] = $tag->name; 
        }
        $single["tags"] = $tags;

        $json[] = $single;
    }
    $json = json_encode($json);

    nocache_headers();
    if (!empty($callback)) {
        header("Content-Type: application/x-javascript; charset=$charset");
        echo "$callback($json);";
    } else {
        **header("Content-Type: application/json; charset=$charset");**
        echo "$json";
    }

} else {
    header("HTTP/1.0 404 Not Found");
    wp_die("404 Not Found");
}
?>
share|improve this question
add comment

1 Answer 1

up vote 1 down vote accepted

Probably get_the_tags() is returning an empty array. Try changing the function to:

$my_tags = get_the_tags();
if ($my_tags){
    foreach($my_tags as $tag) {
        $tags[] = $tag->name; 
    }
}
share|improve this answer
    
I don't know in the last version, but my firefox doesn't show JSON as XML. You can use an extension like: addons.mozilla.org/en-US/firefox/addon/jsonview to see pretty JSON files. –  MZAweb May 9 '11 at 19:10
    
Thanks for the help! –  user1462 May 9 '11 at 19:10
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.