Trying to preg_replace the_content with a custom content type
-
Hi everyone.
I have a custom content type called ‘e-mail template’. Basically we want to use this content type as an assistance tool to generating the contents of html e-mails. This means using external stylesheets in the output is a no-no. The problem I’m running into is that when someone inserts an image into a post, and tells it to align left, that left alignment code isn’t carrying over into the image code, because it’s trying to use a stylesheet to apply it that I’m not implementing on the template.
So, what i’d like to do is run a preg_replace on the_content() to search for “<img class=”alignleft” and replace it with ‘<img style=”float: left; margin: 7px;” class=”alignleft’
I thought this would be fairly easy, but when i run the following code on the template itself, I get no body output.
$findleft = '<img class="alignleft'; $replaceleft = '<img style="float: left; margin: 7px;" class="alignleft'; $replacedcontent = preg_replace($findleft, $replaceleft, the_content()); print $replacedcontent;
So I did some reading and found maybe the way to do this is on my theme’s functions.php
So I tried this:
function emailleftappend($content){ if(get_post_type( $_GET['post']) == 'email-template' ){ $findleft = '<img class="alignleft'; $replaceleft = '<img style="float: left; margin: 7px;" class="alignleft'; $replacedcontent = preg_replace($findleft, $replaceleft, $content); } return $replacedcontent; } add_filter('the_content', 'emailleftappend');
This also produced a blank body, but of course, sitewide.
I’m not sure how to pull this off. It feels like I’m misunderstanding something basic here. Any thoughts?
- The topic ‘Trying to preg_replace the_content with a custom content type’ is closed to new replies.