Skip to content

Instantly share code, notes, and snippets.

@nikolov-tmw
Last active August 29, 2015 14:15
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 nikolov-tmw/3b1c068ada7010296652 to your computer and use it in GitHub Desktop.
Save nikolov-tmw/3b1c068ada7010296652 to your computer and use it in GitHub Desktop.
Inline image replacement
<?php
// This is right after
// if ( $attachment_count > 0 && ! has_shortcode( $post_content, 'gallery' ) ) {
// I realize now that it should actually be in a separate if that doesn't include the ! has_shortcode() condition
// 3D seems to be included when sending email via Apple Mail like so:
// src=3D"...."
preg_match_all( '/<img(.*?)src=3?D?[\'"]cid:([^"\']*)[\'"]([^<>]*)\/?>/ims', $post_content, $inline_image_matches );
if ( $inline_image_matches ) {
$search_array = array();
$replace_array = array();
foreach ( $inline_image_matches[2] as $i => $cid ) {
if ( isset( self::$image_cids[ $cid ] ) ) {
$search_array[] = $inline_image_matches[0][ $i ];
// self::$image_cids[ $cid ][1] is the URL to the "full" size of the image
// self::$image_cids[ $cid ][0] is the URL to the "medium" size of the image or the "full" size if "medium" wasn't found
// we use a filter to let other people change "medium" to another size
$replace_array[] = '<a href="' . esc_attr( self::$image_cids[ $cid ][1] ) . '" target="_blank"><img' . $inline_image_matches[1][ $i ] . 'src="' . esc_attr( self::$image_cids[ $cid ][0] ) . '"' . $inline_image_matches[3][ $i ] . ' /></a>';
}
}
$post_content = str_replace( $search_array, $replace_array, $post_content );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment