Skip to content

Instantly share code, notes, and snippets.

@dboutote
Last active February 13, 2016 02:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dboutote/72de272491da33a02285 to your computer and use it in GitHub Desktop.
Save dboutote/72de272491da33a02285 to your computer and use it in GitHub Desktop.
Encoding HTML in WordPress Posts: https://wordpress.org/plugins/wp-post-encode/
<?php
function dbdb_quick_encode($content_text) {
$charset = get_bloginfo('charset');
$replaced_text = preg_replace('#(<!--encode-->)(.*?)(<!--/encode-->)#isme', "'$1'.str_replace(array('<!--nextpage-->', '<!--more-->'), array('&lt;!--nextpage--&gt;', '&lt;!--more--&gt;'), '$2').'$3'", $content_text);
foreach($replaced_text as $k => $v ) {
$encoded_text[$k] = str_replace(array('"'),array('"'), $v);
}
return $encoded_text;
};
add_filter('wp_insert_post_data','dbdb_quick_encode', 1, 1);
function dbdb_post_encode($text) {
$text = str_replace(array("rn", "r"), "n", $text);
$text = preg_replace_callback("#(<!--encode-->)(.*?)(<!--/encode-->)#is", 'dbdb_code_encode', $text);
return $text;
};
add_filter('the_content','dbdb_post_encode', 1, 1);
function dbdb_code_encode( $matches ) {
$charset = get_bloginfo('charset');
$text = trim($matches[2]);
$text = str_replace(array('&lt;!--nextpage--&gt;', '&lt;!--more--&gt;'), array('<!--nextpage-->', '<!--more-->'), $text);
$text = htmlspecialchars($text, ENT_QUOTES, $charset);
$text = str_replace('[','&#91;', $text);
$text = str_replace(array("rn", "r"), "n", $text);
$text = preg_replace("#nnn+#", "nn", $text);
if ( "<!--encode-->" != $matches[1] ) {
$text = $matches[1].$text.$matches[3];
}
return $text;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment