Skip to content

Instantly share code, notes, and snippets.

@ericmann
Created April 17, 2013 01:04
Show Gist options
  • Save ericmann/5400972 to your computer and use it in GitHub Desktop.
Save ericmann/5400972 to your computer and use it in GitHub Desktop.
<?php
function add_img_titles( $content ) {
global $post;
preg_match_all( '/<img[^>]*>/', $content, $matches );
$replacements = array();
foreach( $matches[0] as $match ) {
// If this image has a title, skip it
if ( preg_match( '/title=\s*/', $match ) ) {
continue;
}
$newimg = str_replace( '<img ', '<img title="' . $post->post_title . '" ', $match );
$replacements[ $match ] = $newimg;
}
foreach( $replacements as $original => $replacement ) {
$content = str_replace( $original, $replacement, $content );
}
return $content;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment