WordPress “Save Draft” fails: “the response is not a valid JSON response”

The reason of this error was stupid. The problem was that when wordpress sent a JSON request, the answer was not in JSON format. There was an echo instead of return in one of my theme functions, under template-tags.php.

This caused each json to take that html out with it.

if ( ! function_exists( 'my_posted_on' ) ) {
    function my_posted_on() {
        $time_string = '';
        // ...
        $posted_on   = apply_filters(
            'my_posted_on', sprintf(
                '<span class="posted-on">...</span>',
                esc_html_x( 'Posted on', 'post date', 'theme' ),
                esc_url( get_permalink() ),
                apply_filters( 'my_posted_on_time', $time_string )
            )
        );
        $byline      = apply_filters(
            'my_posted_by', sprintf(
                '...'
            )
        );
        echo $posted_on . $byline;
    }
}

I replaced echo with return and fixed it.

Leave a Comment