1

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

currently i just need to prevent '&' change 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;");

but that code still encode the html.

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

update[SOLVED]: stackexchange

$content = get_post_field('post_content', XXX, 'raw');
$content = str_replace('amp;', '', $content);
$wpdb->update( $wpdb->posts, array( 'post_content' => $content ), array( 'ID' => XXX ) );
2
  • Just a pointer you might want to try wordpress.stackexchange.com, completely dedicated to WordPress, might be quicker for you :-) Feb 14, 2011 at 9:25
  • thank you. but currently no one give answer :)
    – Andy
    Feb 14, 2011 at 10:39

1 Answer 1

3
<?php $content = htmlentities( html_entity_decode($content) ); ?>

This code will print HTML tags, and then decode HTML entities (like &amp; to &).

Oh, and with wp_insert_post(), do:

// Create post object
$my_post = array(
  'post_content' => $content,
  'post_id' => POST_ID # update the post with the same ID as POST_ID
);

You can use the escape function as a shortcode:

function escape_html_func( $attrs, $content = "" ) {
  return htmlentities( html_entity_decode($content) );
}
add_shortcode( 'escape', 'escape_html_func' );

like [escape]<span>Hello!</span>[/escape] in blog posts. Is this what you meant by content filter?

2
  • Thank you, I didn't try but seems if use wp_insert_post() its still encode it. But in stackexchange has answer it.
    – Andy
    Feb 15, 2011 at 2:14
  • 1
    You're welcome. Saw the answer at Stackexchange, although I think you should use my code with htmlentities instead of a str_replace since it catches more than just "amp;" (and what if you you write amp; in another context?). Anyway, just glad to help. Tried my shortcode?
    – jocap
    Feb 18, 2011 at 14:20

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.