Created
April 8, 2011 17:10
-
-
Save mfields/910297 to your computer and use it in GitHub Desktop.
This file contains 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 | |
$src = ''; | |
$content = get_the_content(); | |
/* Content contains only a url. */ | |
$src = esc_url( trim( $content ) ); | |
if ( ! empty( $src ) && is_array( @getimagesize( $src ) ) ) { | |
var_dump( $src ); | |
} | |
/* Attempt to match the first image tag. */ | |
preg_match( '/<img (.+?)>/', $content, $matches ); | |
/* First image was found, extract source and sanitize. */ | |
$atts = array(); | |
if ( isset ( $matches[1] ) ) { | |
foreach ( wp_kses_hair( $matches[1], array( 'http' ) ) as $attr => $value ) { | |
$atts[$attr] = $value['value']; | |
} | |
if ( isset( $atts['src'] ) && ! empty( $atts['src'] ) ) { | |
$src = esc_url( $atts['src'] ); | |
} | |
if ( ! is_array( @getimagesize( $src ) ) ) { | |
$src = ''; | |
} | |
} | |
print '<pre>'; print_r( $src ); print '</pre>'; | |
print '<pre>'; print_r( $atts ); print '</pre>'; | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment