[wp-hackers] Preventing magic quotes in pre elements

Andrew Shearer ashearerw at shearersoftware.com
Fri Jul 23 03:48:42 UTC 2004


I added the following code to my WordPress installation to stop the 
filters from messing up code blocks. I hereby place it under the GPL.

New functions to be defined near the top of 
wp-include/functions-formatting.php:
---
function wpapplyavoidingpre($text, $functionname) {

     /* Apply the named filter, but don't touch anything inside <pre> 
tags,
     because we can only mess it up. We assume that inside a <pre> tag 
pair is
     code or other preformatted content, and applying such things as 
smart quotes
     will ruin it, and applying <br /> on line breaks will result in 
double spacing.

     This code stops short of a full HTML parser, and could be misled by 
things
     that look like <pre> tags wrapped in HTML comments, but oh well. It 
would
     also be confused by any tags beginning with the letters "pre", but 
that's OK--
     there shouldn't be any.

     ashearer 2004-01-21
     */
     $pos = $lastpos = 0;
     while ($lastpos !== false && ($pos = strpos($text, '<pre', 
$lastpos)) !== false) {
         $text = substr($text, 0, $lastpos)
             . $functionname(substr($text, $lastpos, $pos - $lastpos))
             . substr($text, $pos);
         $lastpos = strpos($text, '</pre>', $pos);
     }
     if ($lastpos !== false) {
         $text = substr($text, 0, $lastpos)
             . $functionname(substr($text, $lastpos));
     }
     return $text;
}

function wpautopavoidingpre($text) {
     return wpapplyavoidingpre($text, 'wpautop');
}

function wptexturizeavoidingpre($text) {
     return wpapplyavoidingpre($text, 'wptexturize');
}

---

And at the bottom of wp-include/vars.php, you can make the last couple 
of add_filter lines into:

// ashearer 2004-01-21 added 'avoidpre'
add_filter('the_content', 'wpautopavoidingpre');
add_filter('comment_text', 'wpautopavoidingpre');

On Jul 22, 2004, at 3:45 PM, Eric A. Meyer wrote:

> Hey gang,
>
>    I've poked around the formatting functions file in an attempt to 
> figure out how to prevent magic quoting for the 'pre' element (and, 
> while I'm at it, 'code' and related inline elements).  I came away 
> with an aching head, and no better clue than when I started.  Anyone 
> want to point me in the right direction?
>
> -- 
> Eric A. Meyer  (eric at meyerweb.com)    http://www.meyerweb.com/eric/
> Principal, Complex Spiral Consulting  http://www.complexspiral.com/
> "CSS: The Definitive Guide," "CSS2.0 Programmer's Reference,"
> "Eric Meyer on CSS," and more   http://www.meyerweb.com/eric/books/
>
> _______________________________________________
> hackers mailing list
> hackers at wordpress.org
> http://wordpress.org/mailman/listinfo/hackers_wordpress.org
>

--
Andrew Shearer
Senior Analyst, Medical Computing
IS Applications Group
Lifespan




More information about the hackers mailing list