First of all, I should say that this is a WordPress based question, which I originally asked on the WordPress StackExchange, here. But I think it's turned into a more PHP-based question so that's why I'm asking here.
So basically, I've written this preg_replace_callback
function, which, on saving/publishing the post, will replace all of the image URLs with URLs from the WP Uploads directory. I've had fleeting success with this; sometimes it works, but only on one of the URLs (in my test examples on my site I have 3 img
tags, each split up with paragraphs.
Here is my code:
add_filter('content_save_pre', 'getpostimgs'); // content_save_pre filter may be depreceated? => http://adambrown.info/p/wp_hooks/hook/content_save_pre
function getpostimgs() {
global $post;
$postContent = $post->post_content;
$content = preg_replace_callback(
'/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', // pattern to match to (i.e the contents of an <img src="..." /> tag)
function ($match) {
$imgURL = $match[1]; // the second array (#1) (0-based) is the array with the URLs in. First array (#0) has the whole img tag in.
$image_data = file_get_contents($imgURL); // Get image data
$filename = basename($imgURL) . "-" . $post->ID . ".jpg"; // Create image file name
if( wp_mkdir_p( $upload_dir['path'] ) ) { // check upload file exists and the permissions on it are correct
$file = $upload_dir['path'] . '/' . $filename;
} else {
$file = $upload_dir['basedir'] . '/' . $filename;
} // save file to server
file_put_contents( $file, $image_data ); // save the file to the server
return $file;
},
$postContent
);
return $content;
}
I've added some comments along the way to hopefully explain what I'm doing at each stage. I'm not a PHP wizard (I mainly do WordPress PHP) so be nice! Also, as I put in the comments, the WordPress filter I'm using, content_save_pre
, which is supposed to edit the content before being saved to the database, has been depreciated. But that's a WordPress issue, so I'll consult the guys on the WordPress Stackexchange about that one.
Anyway, my main problem is that when I hit save, the content is completely wiped. As I said above, I've had fleeting success - sometimes it will replace maybe one of the URLs, other times it won't, and most of the time it simply wipes all the content. I'm assuming there's something wrong with the preg_replace_callback
.
Lastly: as you may have seen from the link I posted to the Wordpress StackExchange (right at the top), I originally coded this up by using a preg_match_all
to find all the image URLs, then used a foreach
loop to go through the array of URLs and had a prey_replace
to replace the URLs. That code is here if you want to take a look at that. I changed it based on the advice on this thread (correct answer). But both methods act pretty much the same.
Thanks for any help :)
EDIT: I've updated my code a little, made a few silly mistakes regarding global variables/variable scope and a few syntax/WP errors. Here's my updated code:
function getpostimgs() {
global $post;
$postContent = $post->post_content;
$content = preg_replace_callback(
'/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', // pattern to match to (i.e the contents of an <img src="..." /> tag)
function ($match) {
global $post, $postContent;
$upload_dir = wp_upload_dir(); // Set upload folder
$imgURL = $match[1];
$image_data = file_get_contents($imgURL); // Get image data
$filename = basename($imgURL) . "-" . $post->ID . ".jpg"; // Create image file name
if( wp_mkdir_p( $upload_dir['path'] ) ) { // check upload file exists and the permissions on it are correct
$file = $upload_dir['path'] . '/' . $filename;
} else {
$file = $upload_dir['basedir'] . '/' . $filename;
} // save file to server
file_put_contents( $file, $image_data ); // save the file to the server
return $file;
},
$postContent
);
return $content;
}
add_filter('content_save_pre', 'getpostimgs'); // content_save_pre filter may be depreceated? => http://adambrown.info/p/wp_hooks/hook/content_save_pre
If I call this function on a single.php page or something, it will work, i.e. the Original Image URLs in the post content which is returned have been replaced by Upload Directory URLs, so the regex and stuff is correct. However, it doesn't work when I try to publish/update a post (the content is usually wiped). Any ideas?