I took the answer from this question, and modified it to my needs a bit, but it's not working obviously.
The plugin I'm modifying, has a function that will 'preclean' the content using the function
function aiosc_preclean_content($string) {
$rplc = array(
"\\'"=>"'",
'\\"'=>'"',
"\\\\"=>"\\",
);
return str_replace(array_keys($rplc),array_values($rplc),$string);
}
This looks for quotes and multiple backslashes and replaces them. Function works fine so I wanted to modify it so that anything inside <code>
tags (html in particular) that has <
and >
will be replaced by <
and >
First I tried the original answer, didn't work. Then I tried my own:
function aiosc_preclean_content($string) {
$rplc = array(
"\\'"=>"'",
'\\"'=>'"',
"\\\\"=>"\\",
"‘"=>"'",
"’"=>"'",
"“"=>'"',
"”"=>'"',
);
$pre_returned_content = str_replace(array_keys($rplc),array_values($rplc),$string);
$text = preg_replace_callback("~<code>(.*?)<\/code>~",'replaceInCode',$pre_returned_content);
// $new_content = htmlspecialchars($text);
return $text;
}
function replaceInCode($match){
$replace = array('<' => '<','>' => '>');
$text = str_replace(array_keys($replace),array_values($replace),$match[1]);
return $text;
}
I added the curly quotes as a prereplace check. Now the problem with this is that it will turn the <code>
tags in <code>
which is not good at all (I need the <code>
tags for the code to work properly).
What am I doing wrong here? I tried adding <code>
tags around the returned text in the callback function for preg_replace_callback
, but that didn't work as well.
EDIT: The weird part is when I try this in sandbox online it works :\
EDIT2
I tried this as well (there is another cleaning function that stips tags)
function aiosc_clean_content($string,$html_tags="", $autop = true) {
$string = aiosc_preclean_content($string);
if(empty($html_tags)) $html_tags = "<b><strong><em><i><br><hr><p><span><small><h1><h2><h3><h4><h5><ul><ol><li><a><del><blockquote><pre><code><script>";
$string = strip_tags($string,$html_tags);
if($autop) $string = wpautop($string);
$text = preg_replace_callback("~<code>(.*?)<\/code>~",'replaceInCode',$string);
return $text;
}
function aiosc_preclean_content($string) {
$rplc = array(
"\\'"=>"'",
'\\"'=>'"',
"\\\\"=>"\\",
"‘"=>"'",
"’"=>"'",
"“"=>'"',
"”"=>'"',
);
return str_replace(array_keys($rplc),array_values($rplc),$string);
}
function replaceInCode($match){
$replace = array('<' => '<','>' => '>');
$text = str_replace(array_keys($replace),array_values($replace),$match[1]);
return '<code>'.$text.'</code>';
}
Still nothing.