4

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 android application json youtube video

3 Answers 3

4

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);
4
  • +1, but no, the rest api returns what it should return, the post_content, if you are not happy with it you should develop your own end point Apr 14, 2018 at 8:04
  • ( post edited) if i try to hide them i will lost youtube video Apr 15, 2018 at 22:06
  • Then to need to write a function to parse the YouTube videos too then
    – Mat
    Apr 15, 2018 at 23:17
  • thinks guys it fix by adding the redix on jquery function because i dont have access to wordpress admin dashboard content_splited= content.replace(/\[\/?et_pb.*?\]/g,'@');' Apr 22, 2018 at 16:03
1

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 );
1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.