PHP: eliminare tutte le thumbnail create da WordPress

Short link

Con PHP possiamo eliminare tutte le thumbnail create da WordPress.

Possiamo usare la seguente funzione:


function delete_wp_thumbs() {
  $base = $_SERVER['DOCUMENT_ROOT'] . '/wp-content/uploads';
  $it = new RecursiveDirectoryIterator( $base, RecursiveDirectoryIterator::SKIP_DOTS );
  $files = new RecursiveIteratorIterator( $it, RecursiveIteratorIterator::CHILD_FIRST );
  $count = 0;
  foreach( $files as $file ) {
    if ( $file->isDir() ) {

    } else {
     $path = $file->getRealPath();
     $name = $file->getFilename();
     if(preg_match('/\d+x\d+\.(jpg|jpeg|gif|png|bmp|tiff)$/', $name)) {
        unlink($path);
        $count++;
     }
    }
  }
  return $count;
}

L'autore

Gabriele Romanato, sviluppatore web full stack specializzato in siti, applicativi web ed e-commerce con Node.js e PHP.