Skip to content

Instantly share code, notes, and snippets.

View RemcoWessels's full-sized avatar

Remco Wessels RemcoWessels

View GitHub Profile
@RemcoWessels
RemcoWessels / wordpress-auto-add-alttext-on-upload.php
Last active January 21, 2022 16:41
WordPress function; As it is important for a11y (accessibility) that images got an ALT text this function adds, on upload, the IPTC description from the image to the "Alternative Text" field within WordPress.
/* Automatically set an image Alt-Text on upload */
add_action( 'add_attachment', 'kreks__add_alt_text_from_image_IPTC_on_upload' );
function kreks__add_alt_text_from_image_IPTC_on_upload( $post_ID ) {
// Check if uploaded file is an image, else do nothing
if ( wp_attachment_is_image( $post_ID ) ) {
$postMeta = get_post_meta($post_ID);
$uploads = wp_get_upload_dir();
$imgURL = $uploads['basedir'] . "/" . $postMeta['_wp_attached_file']['0'];