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


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=("|\')'.
    $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.

| improve this answer | |
  • 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 ).'"'; – Giri Mar 5 '12 at 17:11
  • try 'src="'.get_site_url().'$2'"'. Like I said, you're gonna have to tinker with it a bit. – mor7ifer Mar 5 '12 at 22:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.