Halloween Sale 15% off Ends in See the plans
PHP

JSON Feed for Your Posts

Publishes your latest posts as a JSON Feed at /feed/json/, a modern alternative to RSS supported by many feed readers and easy to use from apps and scripts.

Don’t have WPIDE yet? Install it on your site

Code

add_action( 'init', function () {
    add_feed( 'json', function () {
        $query = new WP_Query( array(
            'posts_per_page'      => (int) get_option( 'posts_per_rss', 10 ),
            'post_status'         => 'publish',
            'ignore_sticky_posts' => true,
            'has_password'        => false,
            'no_found_rows'       => true,
        ) );

        $items = array();

        while ( $query->have_posts() ) {
            $query->the_post();
            $post = get_post();

            $item = array(
                'id'             => get_permalink( $post ),
                'url'            => get_permalink( $post ),
                'title'          => html_entity_decode( get_the_title( $post ), ENT_QUOTES, 'UTF-8' ),
                'content_html'   => apply_filters( 'the_content', get_the_content() ),
                'summary'        => html_entity_decode( wp_strip_all_tags( get_the_excerpt() ), ENT_QUOTES, 'UTF-8' ),
                'date_published' => get_post_time( 'c', true, $post ),
                'date_modified'  => get_post_modified_time( 'c', true, $post ),
                'authors'        => array(
                    array(
                        'name' => get_the_author_meta( 'display_name', $post->post_author ),
                        'url'  => get_author_posts_url( $post->post_author ),
                    ),
                ),
            );

            $tags = get_the_tags( $post->ID );

            if ( $tags && ! is_wp_error( $tags ) ) {
                $item['tags'] = wp_list_pluck( $tags, 'name' );
            }

            if ( has_post_thumbnail( $post ) ) {
                $item['image'] = get_the_post_thumbnail_url( $post, 'large' );
            }

            $items[] = $item;
        }

        wp_reset_postdata();

        $feed = array(
            'version'       => 'https://jsonfeed.org/version/1.1',
            'title'         => get_bloginfo( 'name' ),
            'home_page_url' => home_url( '/' ),
            'feed_url'      => get_feed_link( 'json' ),
            'description'   => get_bloginfo( 'description' ),
            'language'      => get_bloginfo( 'language' ),
            'items'         => $items,
        );

        if ( has_site_icon() ) {
            $feed['icon'] = get_site_icon_url( 512 );
        }

        if ( ! headers_sent() ) {
            header( 'Content-Type: application/feed+json; charset=' . get_option( 'blog_charset' ) );
        }

        echo wp_json_encode( $feed, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT );
    } );
} );

add_action( 'wp_head', function () {
    echo '<link rel="alternate" type="application/feed+json" title="' . esc_attr( get_bloginfo( 'name' ) . ' JSON Feed' ) . '" href="' . esc_url( get_feed_link( 'json' ) ) . '" />' . "\n";
} );

Notes

With pretty permalinks, visit Settings > Permalinks once after activating so the /feed/json/ address works; ?feed=json works right away.

How to add this snippet

  1. Install WPIDE on your WordPress site (it is free) and open Code Snippets.
  2. Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
  3. It is added inactive: review the code and settings, then switch it on.

Have a snippet worth sharing?

Publish it to the library from WPIDE or right here. Sign in with any WordPress site that runs WPIDE, free or Pro. Public snippets are reviewed by the WPIDE team before they appear.

Which site should get this snippet?

Enter the address of your WordPress site. WPIDE opens there in a new tab with the snippet ready to review and save.

Remembered in this browser only, so you are asked once.

Report this snippet

Tell the WPIDE team what is wrong: broken code, something harmful, spam or copied work.