Whether you are a beginning blogger or an Instagram influencer, it is very important for your content to be “up there” in the search results. This not only applies to your posts and articles, but also to your meticulously crafted images. The world’s most famous and popular blogging platform, WordPress, is not prepared “out of the box” to make your images Google-friendly, but can be easily extended to tackle this problem.
Your images are not only pretty pictures for humans to look at and like. Underneath they need to be titled and categorized, so that Google and social networks can snatch them and catalogue and display in a relevant manner. By default, the HTML attributes “title”, and “alt” (alternative text, where to put your own custom tags) are not filled-in by WordPress. This means that although your images appear nicely on your blog or website, they may not easily get to the top of Google’s image searches.
To improve the way WordPress handles your images, just include the four snippets described below in the functions.php of your theme (or better, child theme). You can also download the snippet as text file to paste it easily.
-
Add tags to your images and attachments in WordPress editor
This function, first found here, will add a new text box, “Tags” to all of your images and attachments in the WordPress admin panel.
function add_tags_to_attachments() {
register_taxonomy_for_object_type( 'post_tag', 'attachment' );
}
add_action( 'init' , 'add_tags_to_attachments' );
-
(Optional) Include the images in the archive/summary pages of your WordPress blog.
function category_and_tag_archives( $wp_query ) {
$my_post_array = array('post','page','attachment');
if( $wp_query->get( 'category_name' ) || $wp_query->get( 'cat' ) )
$wp_query->set( 'post_type', $my_post_array );
if( $wp_query->get( 'tag' ) )
$wp_query->set( 'post_type', $my_post_array );
}
-
Include the titles and image tags in the HTML of your posts and pages
This function, first found here, will make WordPress include your titles and tags in the HTML code used to display your images within posts, pages and articles. It will only work from now on. It will not add the title nor the tags to your existing posts, pages or articles. You would have to modify your database in order to achieve this (or go through all of your past images manually).
function inserted_image_titles( $html, $id ) {
$attachment = get_post($id);
$thetitle = $attachment->post_title;
//
$tags_str = '';
$tags_array = wp_get_post_tags($id);
foreach ($tags_array as $tag) {
$tags_str .= $tag->name.' ';
}
if($tags_str != '') {
$tags_str = substr($tags_str, 0, strlen($tags_str)-1);
}
$output = str_replace('<img', '
//
if(strpos($output, 'alt=""')===false) {
$output = str_replace('" width', ' '.$tags_str.'" width', $output);
} else {
$output = str_replace('alt=""', 'alt="'.$tags_str.'"', $output);
}
return $output;
}
add_filter( 'media_send_to_editor', 'inserted_image_titles', 15, 2 );
-
Include the titles and image tags in the HTML of your posts’ featured images
This function, first found here, will make WordPress include your titles and tags in the HTML code used to display your featured images. It will make them present site-wide, including your past posts, pages and articles.
function featured_image_titles($attr, $attachment = null){
$attr['title'] = get_post($attachment->ID)->post_title;
//
$tags_str = '';
$tags_array = wp_get_post_tags($attachment->ID);
foreach ($tags_array as $tag) {
$tags_str .= $tag->name.' ';
}
if($tags_str != '') {
$tags_str = substr($tags_str, 0, strlen($tags_str)-1);
}
$attr['alt'] = (($attr['alt']=='')?'':$attr['alt'].' ').$tags_str;
return $attr;
}
add_filter('wp_get_attachment_image_attributes', 'featured_image_titles', 10, 2);
I hope this helped you to improve your blog and reach more traffic! If you have any comments, please drop them below.
Glad it helped, mate.