import_csv( $filename ); } ?>
log['error'] ) ) : ?>

log['error']; ?>

log['notice'] ) ) : ?>

log['notice']; ?>

Image Alt Manager

Export Image Alts


Import Image Alts

Your CSV file must contain an img_id column. All other columns are optional, but this is generally what it should look like:
img_id url alt_text

Only the alt_text will be updated for each image. If alt_text is left blank, that image will be skipped.

fp = fopen( $filename, 'r' ); if ( false === $this->fp ) { $this->log['error'] = 'Error uploading file.'; return false; } set_time_limit( 0 ); $headers = fgetcsv( $this->fp, 0, ',' ); if ( ! in_array( 'img_id', $headers, true ) ) { $this->log['error'] = 'Please include image IDs in your CSV!'; return false; } while ( false !== ( $row = fgetcsv( $this->fp, 0, ',' ) ) ) { $columns = count( $headers ); $item = new stdClass(); for ( $c = 0; $c < $columns; $c++ ) { $item->{ $headers[ $c ] } = $row[ $c ]; } if ( $item->alt_text && 'attachment' === get_post_type( $item->img_id ) ) { update_post_meta( $item->img_id, '_wp_attachment_image_alt', $item->alt_text ); } } fclose( $this->fp ); $this->log['notice'] = 'Image alts imported.'; } /** * Export CSV of image alts */ public function maybe_export_alts() { if ( ! isset( $_POST['image_alt_export_submit'] ) || ! check_admin_referer( 'image-alt-export' ) ) { return; } $export_type = $_POST['export_type']; $site_name = get_bloginfo( 'name' ); $csv_file = sanitize_title( $site_name ) . '-image-alts.csv'; $args = [ 'post_type' => 'attachment', 'posts_per_page' => -1, ]; if ( 'empty' === $export_type ) { $csv_file = sanitize_title( $site_name ) . '-image-alts-empty.csv'; $args['meta_query'] = [ 'relation' => 'OR', [ 'key' => '_wp_attachment_image_alt', 'compare' => 'NOT EXISTS', ], [ 'key' => '_wp_attachment_image_alt', 'value' => '', ], ]; } $images = get_posts( $args ); // Set headers so browser knows to download CSV header( 'Content-Type: text/csv; charset=utf-8' ); header( 'Content-Disposition: attachment; filename=' . $csv_file ); header( 'Cache-Control: no-cache, no-store, must-revalidate' ); // HTTP 1.1 header( 'Pragma: no-cache' ); // HTTP 1.0 header( 'Expires: 0' ); // Proxies // Create a file pointer connected to the output stream $output = fopen( 'php://output', 'w' ); // Build and send CSV fputcsv( $output, [ 'img_id', 'url', 'alt_text' ] ); foreach ( $images as $img ) { fputcsv( $output, [ $img->ID, wp_get_attachment_url( $img->ID ), get_post_meta( $img->ID, '_wp_attachment_image_alt', true ) ] ); } fclose( $output ); die(); } } /** * Returns the main instance of FXImageAltManager to prevent the need to use globals. * * @return FXImageAltManager */ function FXImageAltManager() { return FXImageAltManager::instance(); } add_action( 'plugins_loaded', 'FXImageAltManager' );