Contact Us

You can call us at +972-2-6483441
or fill out the form below and we will get in touch with you shortly

Your name:

Phone Number:

Email:

Website:

Message:

Please answer this security question
What color is the sky?

Bulk review of Images in wordpress

I recently had a client that had a problem with the images on their site. The person in charge of writing content for the site upload many small, poorly compressed, or otherwise unsuitable images. There are literally hundreds of images like this on the site. So I quickly put together this script to find and delete images on the site:

I made all my code in simple file and included it in functions.php in the themes directory

include("cleanupimages.php");

in cleanupimages.php I added a theme menu. I have the habit of prepending “SEO_Jerusalem_” to everything to avoid any possible naming conflicts.  First I add an theme menu (see here for more information about this):

add_action('admin_menu','SEO_Jerusalem_add_clean_up_image_menu');


function SEO_Jerusalem_add_clean_up_image_menu(){
add_theme_page('Clean Up Image', 'Clean Up Image', 'administrator', 'SEO_Jerusalem_clean_up_images', 'SEO_Jerusalem_clean_up_images');
}

The “Clean Up Images” page is pretty simple. If there is post data it deletes the checked images.  I add the line “ID = -1” to deal with the extra “OR” in the query statement. I am sure that there is a better way to deal with this.


function SEO_Jerusalem_clean_up_images(){
if($_POST) {
global $wpdb;
$query="Delete from ".$wpdb->posts." where ";
$delete_images=$_POST['SEO_Jerusalem_delete_image'];
foreach($delete_images as $delete_image_id => $value ){
$query.='`ID`='.$delete_image_id.' OR ';
}
$query.="`ID`=-1";
if ($wpdb->query($query)){
echo "Files Deleted!";
}
}

then it list all the images


echo "<h2>Clean Up links</h2>";
echo "listing all images that are less than 400x400
";
$args = array(
'post_type' => 'attachment',
'numberposts' => -1,
'post_status' => null,
'post_parent' => null, // any parent
);
$attachments = get_posts($args);
$i=0;
echo '<form name="cleanup_images" method="post" action="">';
if ($attachments) {
foreach ($attachments as $attachment) {
setup_postdata($post);
$data=wp_get_attachment_image_src($attachment->ID, 'full');
$width=$data[1];
$height=$data[2];
if ($width<400 ||$height<400){echo "<input name="SEO_Jerusalem_delete_image[$attachment-&gt;ID]" type="checkbox" />";
echo $i++.') '.$attachment->ID." is too small ($width x $height)";
the_attachment_link($attachment->ID, true);
edit_post_link( "Edit post", "" , "", $attachment->post_parent );
echo "<br />";
}
}
}echo "<p><input name="Submit" type="submit" value="delete images" /></p>
</form>”;

}

you can downloaded the file here: cleanupimages.txt

Rename it to .php and put in your theme folder.  The put “include(‘cleanupimages.php’) in your functions.php file and you should be all set.  Feel free to you this code as you wish, but if you do please link to this post.