As as title, how to prevent wp not to encode html in a post?

currently i just need to prevent '&' changed to & The result need to be looks like on editor with html tab mode selected.

$content = $wpdb->get_row("SELECT post_content FROM $wpdb->posts WHERE ID=xxx");
$content = str_replace('amp;','',$content->post_content);//remove amp;

$wpdb->query("UPDATE $wpdb->posts SET post_content = $content WHERE ID = xxx;");

and also how to implement content filter(prevent to encode some text) in collaboration with wp_insert_post() function

but that code still encode the html.

2 Answers 2


Save post when in HTML tab of editor. It should be saved as close to what you see in editor as possible. I had run couple of quick tests and ampersands are not encoded in this case (as you want to).

I am not sure how to prevent this for visual editor, because a lot of formatting-related stuff in editor is handled by JavaScript rather than PHP.


Cleaning up your snippet, this seems to work:

$content = get_post_field('post_content', XXX, 'raw');
$content = str_replace('amp;', '', $content);
$wpdb->update( $wpdb->posts, array( 'post_content' => $content ), array( 'ID' => XXX ) );
  • Yes i can do it in editor, but i need to run in my code.
    – user3135
    Feb 14, 2011 at 10:47
  • WOW.. Its working, thank you. The key is 'raw' right? Thank you Rarst
    – user3135
    Feb 14, 2011 at 12:02

IMHO, the simplest way to achieve this behaviour is by using a plugin to forcibly save and recall your editor input as raw (and not interpret it upon save/publish); there are a few available in the WP plugins directory but most have particular 'quirks' as to their usage or implementation.

I had particular problems with embedding YouTube and other video/audio players until I utilised a raw HTML plugin.

Your Answer

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