1

I have a post content(html) like this:

<div id="home_tabs">
<ul>
    <li><a href="#tabs-1">Nunc tincidunt</a></li>
    <li><a href="#tabs-2">Proin dolor</a></li>
    <li><a href="#tabs-3">Aenean lacinia</a></li>
</ul>
<div id="tabs-1">
<p>Proin elit arcu, rutrum commodo, vehicula tempus, commodo a, risus.</p>
<p><a class="jqbut" href="#">read more</a></p>
</div>
<div id="tabs-2">Morbi tincidunt, dui sit amet facilisis feugiat, odio metus gravida ante, ut pharetra massa metus id nunc.</div>
<div id="tabs-3">Mauris eleifend est et turpis. Duis id erat. Suspendisse potenti.</div>
</div>

When I'm using echo $post['post_content']; or echo do_shortcode($post['post_content']); the content shows up fine. But when using echo apply_filters('the_content', $post['post_content']); some of the html tags are removed from the post. The output is like this:

<a href="#tabs-1">Nunc tincidunt</a>
<a href="#tabs-2">Proin dolor</a>
<a href="#tabs-3">Aenean lacinia</a>
<p>Proin elit arcu, rutrum commodo, vehicula tempus, commodo a, risus.</p>
<p><a class="jqbut" href="#">read more</a></p>
Morbi tincidunt, dui sit amet facilisis feugiat, odio metus gravida ante, ut pharetra massa metus id nunc.
Mauris eleifend est et turpis. Duis id erat. Suspendisse potenti.

How to get the exact html contents of the post using the content filter?

1

You shouldn't be putting arbitrary HTML into post content, instead use shortcodes! Shortcodes were intended for this situation.

Otherwise you face 2 problems:

  • Filters will escape your content via the wp_kses_post on output
  • TinyMCE will filter/mangle/strip tags when you use the visual editor, even for the slightest of moments

All of these are done for security purposes, content should only contain content, not layout or UI. If you need those, use widgets, or put shortcodes inside your content.

  • Thank you. That's the information I needed. If I put the HTML contents in shortcode/widget, then I wont have the ability to edit it through the TinyMCE visual... am i right? – sariDon May 10 '16 at 20:07
  • If you add code in the TinyMCE Text tab, it will still mess with it when you switch back to the Visual tab. Depends exactly what you're adding though. – Tim Malone May 10 '16 at 20:39
  • Yes, @TimMalone is right. You would need to stick with one tab. Bottom line: avoid HTML in TinyMCE as much as possible. – N00b May 10 '16 at 20:52
  • Thanks for your replies. My problem (as stated in the question) is (already) using with the TinyMCE text tab.... In context to this answer, I just wanted to know whether I can visually alter HTML in widgets/shortcode when I can't place it in TinyMCE. – sariDon May 10 '16 at 20:56

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.