Skip to content

Instantly share code, notes, and snippets.

@mfields
Created April 8, 2011 17:10
Show Gist options
  • Save mfields/910297 to your computer and use it in GitHub Desktop.
Save mfields/910297 to your computer and use it in GitHub Desktop.
<?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