Last active
August 29, 2015 14:15
-
-
Save nikolov-tmw/3b1c068ada7010296652 to your computer and use it in GitHub Desktop.
Inline image replacement
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
<?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