0

In my post there is Visual and Text tabs. I've formatted some content and in the text tab it looks like this:

<strong>The Mission Statement –</strong>
The following is a mission statement.

<strong>Company Information –</strong>
Formed 2016, founded by 

<strong>Growth Highlights –</strong>
Highlights here

When I publish the document and look at the HTML it's changed to this (note the paragraph tags):

<p>
    <strong>The Mission Statement –</strong>
    The following is a mission statement.
</p>

<p>
    <strong>Company Information –</strong>
    Formed 2016, founded by 
</p>

<p>
    <strong>Growth Highlights –</strong>
    Highlights here
</p>

I want to be able to get the published HTML value. I'm using a plugin that gets the raw value of the HTML so it's only returning the first block of code.

Should I manually add BR tags where I encounter line breaks (I really don't want to)? How do I get that formatting back? Why come it don't work?

1

WordPress runs a filter on the content to automatically add paragraphs: wpautop. You can remove the filter from all your post's content if you like:

remove_filter('the_content','wpautop');

Alternatively, you can check out the Disabler plugin which can do this for you.

If you want to remove the filter from particular post(s) you can do:

add_filter('the_content','maybe_remove_wpautop',9);
function maybe_remove_wpautop($content) {
    global $post;
    // add post iDs for which you do not want autoparagraphs
    $nowpautopposts = array('23','47');
    if (in_array($post->ID,$nowpautopposts)) {remove_filter('the_content','wpautop');}
    return $content;
}

It should also be noted that some formatting is processed when you switch between the Text and Visual tabs on the writing screen. This is a much more complex problem, if you want to remove that you can try Preserve HTML Markup Plus plugin.

Update:

After the question was updated, the way to get the processed block of HTML code from the content is to use the following:

// (if you don't already have the post object)
global $post;
$post = get_post($postid);

// apply the content filters to the post object contents
$content = apply_filters('the_content', $post->post_content);

This will apply the existing content filters to the post's content.

I have added the better practice of making sure you set the global $post to the post object from a post ID, because it is entirely possible some plugins could use the $post object inside their filter conditions and you want to make sure (as for example the previous code given needs this.)

On that note sometimes you may want to remove and re-add certain plugin content filters if you want to get the content without them, but you need to be specific. eg. if you ever did want the processed content without the autoparaghs (say if you want to add them yourself a different way) you could do this instead:

remove_filter('the_content','wpautop');
$content = apply_filters('the_content', $post->post_content);
$add_filter('the_content','wpautop');
  • I'm sorry I meant to say it gets the raw value of the content. It gets the code from the first block. How can I get the second block of code? – 1.21 gigawatts Jun 11 '16 at 0:18
  • 1
    oh sorry I getcha, that's easier you can just use $content = apply_filters('the_content',$post->post_content); to apply the existing content filters. – majick Jun 11 '16 at 0:29
  • Awesome thanks. I'll reword the question when I get home. Using the app. – 1.21 gigawatts Jun 11 '16 at 0:31
  • 1
    no probs, updated now and extended it a little bit. :-) – majick Jun 12 '16 at 5:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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