1

I've written a filter function to replace all image URLs in a post with URL from the Uploads directory of my site. However, when using the content_save_pre, filter it won't actually work - but if I use the content_edit_pre filter, it works. Here is my code:

$oldPostContent = $post->post_content; // pass old content into function as argument
function get_the_post_imgs($oldPostContent) {
    global $post;
    $newPostContent = $oldPostContent;
    $newPostContent = preg_replace_callback(
        "/<img.*src=[\"']([^\"']*)[\"'].*>/i", // pattern to match to (i.e the contents of an <img src="..." /> tag)
        function ($match) {
            return "hello world"; // obviously it's a lot more complicated than this but for your sake i've massively simplified it so the src="..." content is just replaced with "hello world" for now
        },
        $oldPostContent
    );

    return $newPostContent; // return new manipulated content
}

add_filter('content_save_pre', 'get_the_post_imgs'); // this doesn't work on the content, but if I use content_edit_pre, it does work

I've added some comments along the way to help you out. I'm pretty sure it's not a problem with the regex (http://regex101.com/r/aP4fU9) so it's something to do with my code not working the way content_save_pre needs it to work.

Thanks for any help :)

  • 2
    There is something wrong with your code. Notice how the syntax highlighter makes everything red? And the sixth line down starts with an unusual character-- I see a square. I don't know if that is an artifact of your pasting here or nor, or if it has anything to do with your problem, but the code is not easily testable until you clean that up. – s_ha_dum Apr 2 '14 at 12:53
  • Not sure what you mean by a square on the sixth line down...that part is the pattern for the regex. It's supposed to read /<img.*src=[\"']([^\"']*)[\"'].*>/i (screenshot for you in case you still have the problem: cl.ly/Uml7) Also in regards to the syntax highlighter making all the code red, any question I go on for the WordPress StackExchange has this problem right now, not sure if it'd be doing it for you as well? :/ – Tom Oakley Apr 2 '14 at 13:26
  • 1
    The character I am talking about is outside the first double quote before the img regex you posted. It is not part of the regex. I have not seen the syntax problem on any question in the last hour or so besides yours. What are you using to edit code? – s_ha_dum Apr 2 '14 at 13:35
  • Still can't see it, sorry :/ I use Coda 2 to write my code and Safari on Mac OS X Mavericks to post here. What browser are you using? – Tom Oakley Apr 2 '14 at 13:50
  • This is a screenshot of my text editor (vi), into which I've pasted your code. Note the weird... thing at the start of the regex. dropbox.com/s/756hwq12qo6t875/… – Pat J Apr 2 '14 at 14:04
2

I copied your code to a file in my home directory on my Ubuntu box, removed the weird character that @s_ha_dum noticed, and ran it. It worked as I'd expect.

Code:

<?php

function get_the_post_imgs($oldPostContent) {
    $newPostContent = $oldPostContent;
    $newPostContent = preg_replace_callback(
        "/<img.*src=[\"']([^\"']*)[\"'].*>/i", // pattern to match to (i.e the contents of an <img src="..." /> tag)
        function ($match) {
            return "hello world"; // obviously it's a lot more complicated than this but for your sake i've massively simplified it so the src="..." content is just replaced with "hello world" for now
        },
        $oldPostContent
    );

    return $newPostContent; // return new manipulated content
}

echo get_the_post_imgs( '<a href="foo.php"><img align="right" src="http://example.com/fire.gif" /></a><br />' );

?>

Running this code from the command line:

% php -e foo.php 
<a href="foo.php">hello world

Your regex might be a little greedy for your purposes, but it works.

PS, trying to run your code with the weird character still in place:

% php -e foo.php
PHP Parse error:  syntax error, unexpected '"/<img.*src=[\"']([^\"']*)[\"'' (T_CONSTANT_ENCAPSED_STRING) in /home/username/foo.php on line 6

Edit -- Since you asked in the comments, I thought I'd try to cobble up a less greedy regex for you. Try this on for size:

<img.*src=[\"']([^\"']*)[\"'][^>]*>

The extra [^>*] term at the end ends the <img...> tag at its closing >, instead of carrying forward to the last > in the line (in my example, the closing > in the </a> tag).

  • hey, thanks for this :) I put my code in Vim and found the weird little character, deleted it and (after a bit of further tweaking), it works :) However, as you said, the regex is a bit greedy and rather than just replacing the src="..." tag content, it replaces the whole img tag. If I use a regex checker (regex101.com/r/aC0xR7) it says that only the src tag contents is matched, but Wordpress does otherwise. Any ideas how to fix this? Thanks for the help so far :) – Tom Oakley Apr 2 '14 at 15:56
  • hey Pat, thanks for looking into the Regex :) Your suggested Regex didn't quite work but it set me on the right track - I ended up using this: <img.*src=[\"']([^\"']*). It takes the <img src="..."but stops there so all I have to do is return <img src="along with the new URL and everything's good. Thanks for your help :) – Tom Oakley Apr 2 '14 at 21:11

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.