Skip to content

Instantly share code, notes, and snippets.

@jdevalk
Created June 15, 2012 13:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jdevalk/2936443 to your computer and use it in GitHub Desktop.
Save jdevalk/2936443 to your computer and use it in GitHub Desktop.
If title attribute == alt attribute, strip title attribute.
<?php
if ( preg_match_all( '/<img [^>]+>/', $post->post_content, $matches ) ) {
$content_changed = false;
foreach ( $matches[0] as $img ) {
preg_match( '/title=("|\')([^"\']+)("|\') /', $img, $title_match );
preg_match( '/alt=("|\')([^"\']+)("|\') /', $img, $alt_match );
if ( isset($title_match[2]) && isset($alt_match[2]) && $title_match[2] == $alt_match[2] ) {
$post->post_content = str_replace( $title_match[0], '', $post->post_content );
$content_changed = true;
}
}
if ( $content_changed ) {
wp_update_post( $post );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment