Skip to content

Instantly share code, notes, and snippets.

@TimBHowe
Last active October 29, 2015 13:04
Show Gist options
  • Select an option

  • Save TimBHowe/a4067abc9f634c7f282e to your computer and use it in GitHub Desktop.

Select an option

Save TimBHowe/a4067abc9f634c7f282e to your computer and use it in GitHub Desktop.
Parse through wordpress post content and update the image source URLs to lowercase
<?php
//http://wordpress.stackexchange.com/questions/104605/update-image-path-with-words-starting-uppercase-to-lowercase-chars
//make located image source lowercase
function replace_cb($matches) {
if (!empty($matches[1])) {
return 'src="'.strtolower($matches[1]).'"';
} else {
return $matches[0];
}
}
//Query all the post content and parse image source to make it lowercase
function update_src_case(){
global $wpdb;
$posts = $wpdb->get_results("SELECT ID,post_content FROM {$wpdb->posts} WHERE post_type='post'");
$pattern = '/src="([^"]*)"/i';
foreach ($posts as $p) {
define( 'DOING_AUTOSAVE', true );
$p->post_content = preg_replace_callback( $pattern, 'replace_cb', $p->post_content );
//var_dump($p->post_content);
wp_update_post($p);
}
}
update_src_case();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment