Is there a way to automatically assign thumbnails to all files in Wordpress Download Manager using ImageMagick? -
i'm using wpdm pro, , have 1200 files, images (psd, pdf, eps, jpg). there way automatically assign thumbnails files in wordpress download manager using imagemagick?
found solution here fix details , works charm!
<?php // posts. $allposts = get_posts(array('numberposts' => -1, 'post_type' => 'wpdmpro')); // specify images located. $themepath = get_theme_root().'/'.get_stylesheet().'/thumbs/'; // uploads directory blog. $uploads= wp_upload_dir(); // list of images including extensions. $images = listimages($themepath,true); // list of images without extensions. $imagenames = listimages($themepath,false); function reverseslug($string){ $string = str_replace("-", " ", $string);// convert hyphen space $string = ucwords($string);// capitalize beginning of each word return $string; } // retrieve images specified directory. // output array , without file extensions. function listimages($dirname=".",$display) { $ext = array("jpg", "png", "jpeg", "gif"); $files = array(); if($handle = opendir($dirname)){ while(false !== ($file = readdir($handle))){ for($i=0;$i<sizeof($ext);$i++){ if(strstr($file, ".".$ext[$i])){ $files[] = $file; } } } closedir($handle); } sort($files); foreach($files $thefile){ $info = pathinfo($thefile); $filename = basename($thefile,'.'.$info['extension']); $files1[] = $filename; } if($display == false){ return ($files1); } if($display == true){ return($files); } } for($i = 0; $i < count($allposts); $i++){ // check if post slugs match image slugs. if (is_array($imagenames)) { $check[$i] = in_array($allposts[$i]->post_name, $imagenames); } else { echo 'error'; }; if($check[$i] == 1){ echo 'yes, post title matches image name.<br />'.php_eol; // search through image slugs direct match post slug. $search[$i] = array_search($allposts[$i]->post_name, $imagenames); $filename = $images[$search[$i]]; $newfile = $uploads['path'].'/'.$filename; // copy image theme folder uploads directory. copy($themepath.$filename, $newfile); // delete image theme folder. unlink($themepath.$filename); // retrieve file type file name. $wp_filetype = wp_check_filetype(basename($filename), null); // construct attachment array. $attachment = array( 'post_mime_type' => $wp_filetype['type'], 'guid' => $uploads['url'].'/'.$filename, 'post_title' => preg_replace('/\.[^.]+$/', '', reverseslug(basename($filename))), 'post_content' => '', 'post_status' => 'inherit' ); // function inserts attachment media library. $attach_id = wp_insert_attachment($attachment, $newfile, $allposts[$i]->id); // must first include image.php file // function wp_generate_attachment_metadata() work. require_once(abspath . 'wp-admin/includes/image.php'); // function generates metadata image attachment. // creates thumbnail , other intermediate sizes // of image attachment based on sizes defined on // settings_media_screen. $attach_data = wp_generate_attachment_metadata($attach_id, $newfile); if(!is_wp_error($attach_id)){ // update metadata attachment. wp_update_attachment_metadata($attach_id, $attach_data); // updates value of existing meta key (custom field) specified post. update_post_meta($allposts[$i]->id, '_thumbnail_id', $attach_id); } } else{ echo 'no matches found.<br />'.php_eol; } } ?>
Comments
Post a Comment