• Resolved evandunn

    (@evandunn)


    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?

Viewing 7 replies - 1 through 7 (of 7 total)
  • Joy

    (@joyously)

    All filters must return the variable. Your return is in an if statement. I should say that your variable is only changed in the if statement.

    Joy

    (@joyously)

    function emailleftappend($content){
    	if(get_post_type( $_GET['post']) == 'email-template' ){
    		$findleft = '<img class="alignleft';
    		$replaceleft = '<img style="float: left; margin: 7px;" class="alignleft';
    		$content = preg_replace($findleft, $replaceleft, $content);
    		}
    	return $content;
    }
    add_filter('the_content', 'emailleftappend');
    Thread Starter evandunn

    (@evandunn)

    Good to know!

    Unfortunately, I’m still getting blank pages when I put this into my theme’s functions.php file. I get the header and footer, but no content. Not sure what I’m doing wrong.

    Maybe I’m not pulling the_content() ‘s content into the function correctly?

    • This reply was modified 6 years ago by evandunn.
    Thread Starter evandunn

    (@evandunn)

    I’ve revised the code to the following after doing some more reading:

    add_filter( 'the_content', 'emailleftappend');
    function emailleftappend( $content ) {
    	if(get_post_type( $_GET['post']) == 'email-template' ){
    		$findleft = '<img class="alignleft';
    		$replaceleft = '<img style="float: left; margin: 7px;" class=\"alignleft';
    		//$content = preg_replace($findleft, $replaceleft, $content);
    		if ( is_single() && in_the_loop() && is_main_query() ) {
    			$content = preg_replace($findleft, $replaceleft, $content);
        }
    		}
    	return $content;
    	}

    It outputs the body when I comment out $content = preg_replace($findleft, $replaceleft, $content); but when I have it like this, it still returns a blank body in it’s place. I can tell the content type filter is working now at least, because only the email-template pages are being effected.

    Dion

    (@diondesigns)

    You aren’t using regular expressions so preg_replace isn’t needed. Try the following instead:

    function emailleftappend($content) {
    	if( is_single() && in_the_loop() && is_main_query() && get_post_type($_GET['post']) == 'email-template' ) {
    		return str_replace('<img class="alignleft', '<img style="float:left;margin:7px" class="alignleft', $content);
    	}
    
    	return $content;
    }
    add_filter('the_content', 'emailleftappend');
    Moderator bcworkz

    (@bcworkz)

    Right, with preg_replace(), a regexp is not optional. Providing a normal string to preg_replace() will almost certainly be misinterpreted. Furthermore, str_replace() is much more efficient. preg_replace() is good when you must match strings with varying content.

    If dealing with existing content is not an issue, and all images are inserted through the media library, there’s a much better option — the ‘image_send_to_editor’ filter. This way, the tag is corrected during insertion. Much more efficient than searching content with every request.

    With any existing content, it’s better to do a one time search and replace of stored content (combined with altering on insertion) than doing so for every request. But if what you have is already working well for you, it’s hard to justify changing it except as a learning exercise or a manic pursuit of efficiency ??

    Thread Starter evandunn

    (@evandunn)

    Thanks so much guys! That makes a ton of sense.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Trying to preg_replace the_content with a custom content type’ is closed to new replies.