Skip to content

Instantly share code, notes, and snippets.

@michael-cannon
Created November 6, 2012 14:48
Show Gist options
  • Save michael-cannon/4025132 to your computer and use it in GitHub Desktop.
Save michael-cannon/4025132 to your computer and use it in GitHub Desktop.
Import image keywords as alternate text and tags in WordPress - makes tags available in attachments
function register_attachment_taxonomy() {
add_post_type_support('attachment', 'post_tag');
register_taxonomy_for_object_type('post_tag', 'attachment');
}
add_action('admin_init', 'register_attachment_taxonomy');
// add_filter('wp_read_image_metadata', 'read_all_image_metadata', '', 3);
function add_attachment_post_tags( $meta, $attachment_id ) {
if ( isset( $meta['image_meta']['keywords'] ) )
wp_add_post_tags( $attachment_id, $meta['image_meta']['keywords'] );
return $meta;
}
add_filter('wp_generate_attachment_metadata', 'add_attachment_post_tags', '', 2);
function add_attachment_alt_text( $meta, $attachment_id ) {
if ( isset( $meta['image_meta']['title'] ) ) {
$keywords = isset( $meta['image_meta']['keywords'] ) ? trim( $meta['image_meta']['keywords'] ) : false;
$title = $meta['image_meta']['title'];
$alt_title = ( $keywords ) ? $keywords : $title;
update_post_meta( $attachment_id, '_wp_attachment_image_alt', addslashes($alt_title) );
}
return $meta;
}
add_filter('wp_generate_attachment_metadata', 'add_attachment_alt_text', '', 2);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment