Created
November 1, 2013 21:46
-
-
Save FriendlyWP/7272441 to your computer and use it in GitHub Desktop.
Automatically add 'alt' tags to images if none exist. (Credit: https://gist.github.com/svil4ok/1991931)
This file contains hidden or 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
| // AUTOMATICALLY ADD ALT TAGS TO IMAGES | |
| add_filter('the_content', 'add_alt_tags', 99999); | |
| function add_alt_tags($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="'.$post->post_title.'"', $images[0][$index]); | |
| $content = str_replace($images[0][$index], $new_img, $content); | |
| } | |
| } | |
| } | |
| return $content; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment