0

I'm using a plugin called Foliopress WYSIWYG. While using this plugin, images found in the_content loop using relative image path instead of absolute paths. So all the feed images looks broken.

This plugin stores images in http://mysite.com/images folder instead wp-content/uploads folder. So i would like to use filter to replace relative path.

I mean if any img src file looks like /images/2010/03/example.jpg then it should be filtered and replaced with http://mysite.com/images/2010/03/example.jpg

Can anyone give me the snippet? Thanks

1 Answer 1

1

Something like this should do it, you may have to fiddle with the preg_replace() to get it to work the way you need, but the concept (and, more importantly imo, the regex, is there).

if( is_single() ) {
    add_filter( 'the_content', 'wpse44503_filter_content' );
}

function wpse44503_filter_content( $content ) {
    $regex = '#src=("|\')'.
        '(/images/(19|20)(0-9){2}/(0|1)(0-9)/[^.]+\.(jpg|png|gif|bmp|jpeg))'.
        '("|\')#';
    $replace = 'src="'.get_site_url( $2 ).'"';

    $output = preg_replace( $regex, $replace, $content );

    return $output;
}

That's untested, and I wrote it right into the solution box, so make sure you debug thoroughly and such.

2
  • Thanks for your answer. However i'm getting this error. syntax error, unexpected T_LNUMBER, expecting T_VARIABLE or '$' Can you fix it? This is the line where i'm getting that error $replace = 'src="'.get_site_url( $2 ).'"'; Commented Mar 5, 2012 at 17:11
  • try 'src="'.get_site_url().'$2'"'. Like I said, you're gonna have to tinker with it a bit. Commented Mar 5, 2012 at 22:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.