Created
November 6, 2012 14:48
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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