FAQ’s

How to remove Divi shortcodes from WordPress Rest API (v2) JSON Response Output

The default endpoints of the WordPress REST API are designed to return data that provides for the majority of sites and use cases, but there are often situations where you will need to access, modify or expose additional data in the responses for various object types.

In our case we utilize the Divi page builder to design our pages but we did not want any Divi shortcodes to be exported in the JSON responses. The below function utilizes the rest_prepare_post filter to remove any instances of Divi shortcodes int he rest api response. You can place this code in your themes functions.php file. It can easily be modified for other page builders like visual composer or other use cases.

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

Notes:


Still have questions?
Send us a question at info at adultwebhost1 dot com.