Altering the post content before output

10 views
Skip to first unread message

John Davies

unread,
Mar 7, 2012, 4:11:22 AM3/7/12
to manchester-word...@googlegroups.com
Hi,
Can anyone see what I am doing wrong please? In my loop I am calling
the_content() to output the content of my post, I am trying to strip
out any divs that the user may accidentally put in, so I wrote a
function to do this and called in the filter. Unfortunately the
function is never called by the_content(). If I replace the_content
with echo modify_content(get_the_post()); this will does strip out the
divs, but I don't really want to do it this way.

add_filter( "the_content","modify_content",1 );

function modify_content($content) {
$patterns = array();
$patterns[0] = '/<div.*?>/';
$patterns[1] = '/<\/div>/';
$replacements = array();
$replacements[2] = '';
$replacements[1] = '';
$content=preg_replace($patterns, $replacements, $content);
return $content;
}

--
John Davies

Mike Little

unread,
Mar 7, 2012, 6:29:19 AM3/7/12
to manchester-word...@googlegroups.com
Hi John,

Your code works for me if I drop it into  the bottom of functions.php in twentyeleven on  new WP installation. The actual replacement works by side effect not be design, but I'll get to that in a moment.

Where is your code located? Is it in your theme's functions.php or is it in a plugin. The only reason for that code not being called is if the call to add_filter happens too late, or there is some code somewhere removing all filters on 'the_content'.

Check that your call to add_filter is being called (perhaps bracket it with error_log calls). 

My ealrier point about the actual code: Your replacement array doesn't line up with the pattern array: array indexes 0 and 1 vs 1 and 2!. But because you are replaceing with empty strings and that's the default behaviour, it works. 
You could in fact use this code.


add_filter( "the_content","modify_content", 1);
function modify_content( $content ) {
$patterns = array( '/<div.*?>/', '/<\/div>/' );
$content = preg_replace( $patterns, array(), $content );
return $content;
}


Mike
--
Mike Little
http://zed1.com/


John Logsdon

unread,
Mar 7, 2012, 7:01:17 AM3/7/12
to manchester-word...@googlegroups.com
You can do this in one go:

while (preg_match("/<div[*>]*>/",$content){
$content=preg_replace("/<div[^>]*>([^<\/div>]*)<\/div>/","\\1",$content);
}

ie it will remove all <div ...> ....<div> </div> </div> but actually
wrongly pair them to start with.

I think! Haven't tried it but I did wonder whether your original coding would
work with nested <div>s. A naughty blogger might like to test it!

--
Best wishes

John Logsdon

John Logsdon

unread,
Mar 7, 2012, 7:14:06 AM3/7/12
to manchester-word...@googlegroups.com
Oops

On reflection this will remove the first two <div>s then the first </div> so
you will be left with a </div>. It is probably better to parse the text so
that the pairs are correctly removed....:-( So that would mean using two
arrays and reversing the second one... A bit more messy but otherwise as I
say some enterprising person may try to break it.

So why not use

$content=strip_tags($content,"<div>");

anyway?

John Davies

unread,
Mar 7, 2012, 5:46:25 PM3/7/12
to manchester-word...@googlegroups.com

Hi, Thanks to both Mike and John for your replies to this problem, again much appreciated. The solution, as Mike pointed out, was in fact simply to move the code out of my plugin and into my theme functions file and it worked a treat.

 

John

--
See the group blog at http://mwug.info
 
You received this message because you are subscribed to the Google
Groups "Manchester WordPress User Group" group.
To post to this group, send email to
manchester-word...@googlegroups.com
To unsubscribe from this group, send email to
manchester-wordpress-...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/manchester-wordpress-user-group

Mike Little

unread,
Mar 7, 2012, 6:11:44 PM3/7/12
to manchester-word...@googlegroups.com
On Wed, Mar 7, 2012 at 22:46, John Davies <johnamaz...@gmail.com> wrote:

Hi, Thanks to both Mike and John for your replies to this problem, again much appreciated. The solution, as Mike pointed out, was in fact simply to move the code out of my plugin and into my theme functions file and it worked a treat.

 

John



It should also work from a plugin, but without knowing the details and seeing the code, it is difficult to know what was going wrong. My guess is that the add_filter() call was executing at the wrong time, possibly too early.
 
Mike
Reply all
Reply to author
Forward
0 new messages