WordPress REST API - JSON "Rendered" Content Incorrect

i'm working in android application that use wordpress rest api to get blog from website to , i don't have any knowledge about php or wordpress , but i take some time to learn about it , any way my problem is on the json .the content contain paragraphs unknown and i don't know how to solve this problem ,please help

Topic post-content rest-api json content Wordpress

Category Web


Thanks to the other answers, I solved it like this (in my WP theme functions.php file) :

add_filter( 'rest_prepare_post', 'lb_filter_post_content', 11, 3 );

function lb_filter_post_content($data, $post, $context){
    // remove shortcode just on content > rendered
    $data->data["content"]["rendered"] = preg_replace('/\[\/?et_pb.*?\]/', '', $data->data["content"]["rendered"]);
    return $data;
}

PS. The shortcodes with "et_pb" derive from the fact that the Divi theme is used on the WP with its page builder, so I just remove those shortcodes. I wrote the above in the functions.php of my Divi child-theme. This solution is good if you want to fix the WP side problem, which is where the problem is generated.


You can place this code in your themes functions.php file:

function awh_filter_post_json( $data, $post, $context ) {
    $data = json_encode($data); //convert array or object to JSON string
    $data = preg_replace('/\[\/?et_pb.*?\]/', '', $data); //remove shortcodes
    $data = json_decode($data); //convert JSON String to array or object
    return $data;
}

add_filter( 'rest_prepare_post', 'awh_filter_post_json', 10, 3 );

The content that you are referring to is coming from the Elegant Themes Page Builder plugin on that site.

The page builder uses WordPress Shortcodes to render the content on the WordPress site. However, when you use the REST API, the content is pulled from the WordPress database and the shortcodes are not processed/rendered first.

You would need to remove the shortcodes from the returned JSON before it is displayed in your app. You could use something like this to remove the shortcodes before displaying your content in the app:

// Remove Divi/ET Page Builder shortcodes
$content = preg_replace('/\[\/?et_pb.*?\]/', '', $content);

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.