Created
December 24, 2013 03:02
-
-
Save tommaitland/8108260 to your computer and use it in GitHub Desktop.
A very crude WordPress plugin to search through imported content and replace full size images with a resized counterpart.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Plugin Name: Bulk Attachment Resize | |
Description: Destructive operations on DB to replace full images with their resized version. | |
Author: Agency | |
Version: 1.0.0 | |
Author URI: http://agency.sc | |
*/ | |
if ($_GET['dev']) { | |
add_action('template_redirect', 'myoutput'); | |
function myoutput() { | |
$args = array( | |
'post_type' => 'post', | |
'post_status' => 'publish', | |
'posts_per_page' => '1' | |
); | |
$the_query = new WP_Query( $args ); | |
if ($the_query->have_posts()) { | |
$i = 0; | |
while ($the_query->have_posts()) { | |
$the_query->the_post(); | |
$content = get_the_content(); | |
preg_match_all( '/<img[^>]+src\s*=\s*["\']?([^"\' ]+)[^>]*>/', $content, $matches ); | |
foreach ($matches[1] as $match) { | |
//print $match; | |
$id = get_image_id($match); | |
$large = wp_get_attachment_image_src($id, 'large'); | |
$content = str_replace($match, $large[0], $content); | |
} | |
echo $content; | |
$i++; | |
} | |
} | |
wp_reset_postdata(); | |
exit; | |
} | |
} | |
function get_image_id($image_url) { | |
global $wpdb; | |
$prefix = $wpdb->prefix; | |
$attachment = $wpdb->get_col($wpdb->prepare("SELECT ID FROM " . $prefix . "posts" . " WHERE guid='%s';", $image_url )); | |
return $attachment[0]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment