1

I'm trying to get rid of the shortcodes that wordpress gives me when i use the WP REST API. I already managed to remove the HTML tags by editing the class-wp-rest-posts-controller.php by using strip_tags(). But i don't seem to find a solution for the short codes.

example of the content i get back from the API with the shortcodes

I tried several solutions that did not work. like write this code on the functions.php

// Removes Divi shortcodes
$content = preg_replace('/\[\/?et_pb.*?\]/', '', $content);

or that

$content = strip_tags( do_shortcode( $post->post_content ) );

I'm maybe doing something wrong I'm kinda new at this. if you guys could help me it would be awesome.

3
  • 2
    Never edit core WordPress files. That's never the correct answer for anything. Commented Sep 28, 2021 at 14:09
  • Jacob is right, editing WP core files is one of the worst things you can do and can have disastrous consequences. Also it'll be overwritten when you next update. It's also unnecessary for what you want. It sounds like you want the fully rendered/processed content not the raw content?
    – Tom J Nowell
    Commented Sep 28, 2021 at 14:19
  • yeah, exactly without the [et_pb_section....etc], just the html tags and their contents, i found a way to do it but i edited the core WordPress files, if you have a solution where i don't have to do it, it would be fantastic. Commented Sep 29, 2021 at 6:31

1 Answer 1

1

This is unnecessary, WordPress already provides the fully rendered HTML content in REST responses with the shortcodes already processed and turned into HTML. Inside the object the API responds with is a content field that has a rendered sub-field containing what you want. Use that instead of the raw unprocessed version.

Your Answer

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

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