Skip to content

Instantly share code, notes, and snippets.

@m-pokrovskii
Created June 8, 2015 06:25
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 m-pokrovskii/80a5eb719e08c4b35ef4 to your computer and use it in GitHub Desktop.
Save m-pokrovskii/80a5eb719e08c4b35ef4 to your computer and use it in GitHub Desktop.
Get all images id from post_content.
function insertedImage_save_meta($post_id) {
$upPost = get_post($post_id);
$rawPostDoc = new DOMDocument();
@$rawPostDoc->loadHTML($upPost->post_content);
$imgs = $rawPostDoc->getElementsByTagName('img');
foreach($imgs as $img){
$imgIDStr = substr($img->getAttribute('class'), (stripos($img->getAttribute('class'),'wp-image-')+9), 8);
$imgID = preg_replace("[^0-9]", '', $imgIDStr);
if($imgID !== false && $imgID !== '') { // double falsy check because of specific nature of stripos() returns coupled with the preg_replace return. Not sure if this is necessary.
$imgIDs[] = $imgID;
}
}
return $imgIDs;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment