How to get_the_content with proper formatting inside functions.php?
Author: Dimitar Radev | Posted on: July 24, 2018 |

To get the post or page content inside WordPress functions.php with all the HTML tags you need to use the_content filter.
This method of getting the content data is very helpful when creating custom REST API points or Plugins.
global $post; $my_variable = apply_filters('the_content', get_post_field('post_content', $post->id));
TL;DR
You have probably landed here after browsing the WEB for “get_the_content() is not formated properly” or something similar. The reason is that get_the_content() is not passed
through the ‘the_content’ filter. Wich means that $my_variable = apply_filters(‘the_content’, get_the_content($post->id)); will work as well. The reason I’m using get_post_field(‘post_content’, $post->id) is because sometimes in get_the_content() and the_content()  functions a variable sent to a PHP count() could be NULL and that is an issue for PHP 7.2
If you want to recieve the latest WordPress snippets, tutorials, tricks and useful information about web development, seo, social media marketing, google you can Subscribe to our newsletter. All you need to do is to fill the Subscribe form below, verify your Free Subscription in your email and start read our daily useful tips and tricks straight in your mailbox.
Note* we hate spam and our newsletter is powered by Google Feedburner and you will not get spam messages in your mailbox.
Leave a Reply