Created
June 8, 2015 06:25
-
-
Save m-pokrovskii/80a5eb719e08c4b35ef4 to your computer and use it in GitHub Desktop.
Get all images id from post_content.
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
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