StudioPress Community Forums

StudioPress Community Forums (http://www.studiopress.com/support/index.php)
-   General Discussion (http://www.studiopress.com/support/forumdisplay.php?f=7)
-   -   Beter image SEO function (http://www.studiopress.com/support/showthread.php?t=124050)

Markoi 11-11-2012 04:33 AM

Beter image SEO function
 
Could someone help me with some code to drop in my functions.php to have beter image SEO.

When i write a post i always set a custom image title tag for my images.

But what i want is when the alt tag is empty the image title tag is taken to set the alt tag.

This function isn't working:

/** Auto-Generate ALT tag for images */
function image_alt_tag($content)
{global $post;preg_match_all('/<img (.*?)\/>/', $content, $images);
if(!is_null($images)) {foreach($images[1] as $index => $value)
{if(!preg_match('/alt=/', $value)){
$new_img = str_replace('<img', '<img alt="'.get_the_title().'"', $images[0][$index]);
$content = str_replace($images[0][$index], $new_img, $content);}}}
return $content;
}
add_filter('the_content', 'image_alt_tag', 99999);

NicktheGeek 11-12-2012 09:23 AM

Why go through all this extra work instead of just writing good alt tags. Alt tags are much more important to SEO than a title tag so that should be the best practice for adding images. Additionally, alt tags really shouldn't be the same as title tags.


All times are GMT -5. The time now is 05:29 PM.

Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.