Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

share|improve this question

1 Answer 1

up vote 1 down vote accepted

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.

share|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. –  m0r7if3r Mar 5 '12 at 22:59

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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