i want to get a post embedded images but the only methods i see over the internet are
$attachments = get_posts(array(
'post_parent' => get_the_ID(),
'post_type' => 'attachment',
'posts_per_page' => $n,
'post_mime_type' => 'image'
));
$attachments = get_children(array(
'post_parent' => get_the_ID(),
'post_type' => 'attachment',
'posts_per_page' => $n,
'post_mime_type' => 'image'
));
$attachments = get_attached_media('image', get_the_ID());
but this gives only the attached images i want all the images in a post even if they are not attached to that post 'post_parent' => get_the_ID()
i think this is the wordpress way of attaching images it sounds like a one-to-many relationship an image can only be attached to one post
any idea ? thanks in advance
i want to get a post embedded images but the only methods i see over the internet are
$attachments = get_posts(array(
'post_parent' => get_the_ID(),
'post_type' => 'attachment',
'posts_per_page' => $n,
'post_mime_type' => 'image'
));
$attachments = get_children(array(
'post_parent' => get_the_ID(),
'post_type' => 'attachment',
'posts_per_page' => $n,
'post_mime_type' => 'image'
));
$attachments = get_attached_media('image', get_the_ID());
but this gives only the attached images i want all the images in a post even if they are not attached to that post 'post_parent' => get_the_ID()
i think this is the wordpress way of attaching images it sounds like a one-to-many relationship an image can only be attached to one post
any idea ? thanks in advance
Share Improve this question edited Mar 11, 2021 at 18:33 fuxia♦ 107k38 gold badges255 silver badges459 bronze badges asked Mar 11, 2021 at 16:22 mod7exmod7ex 1233 bronze badges 1- If you mean all embedded images from post content, maybe you can try a regex in post content to search all images. Also you should look for all post meta in a same way. But if any post meta has attachment ID instead of URL, it can be quite impossible unless you explicitly know which meta key contains an attachment ID. – Sohan Zaman Commented Mar 11, 2021 at 17:55
2 Answers
Reset to default 1i think if WordPress doesn't provide such a function then this is the shortest way
function my_get_embeded_media() {
$content = apply_filters('the_content', get_the_content());
$arr = preg_match_all("/<img[^>]* src=\"([^\"]*)\"[^>]*>/", $content, $matches);
return $arr ? $matches[1] : array();
}
but we still can't control the images size!
You can use DOMDocument to get every image from any page:
function testingdom(){
$dom = new DOMDocument();
libxml_use_internal_errors(true);
$dom->loadHTMLFile('https://the_post_url/anyone');
$data = $dom->getElementsByTagName("img");
$srcs = array();
foreach($data as $key => $dat){
$srcs[] = $data->item($key)->getAttribute("src");
}
$dom = null;
}
add_action("wp_head", "testingdom");
This way you should have every src in an array called srcs