Skip to content

Instantly share code, notes, and snippets.

@davidsword
Created February 8, 2018 20:54
Show Gist options
  • Save davidsword/dc7071b68e15331cb5482217e2354ff0 to your computer and use it in GitHub Desktop.
Save davidsword/dc7071b68e15331cb5482217e2354ff0 to your computer and use it in GitHub Desktop.
two functions to: extract a posts media items, and extract all library items. can use to compare
<?php
function myplugin_find_media($content) {
$mediaRegex = "/(src|href)=\"(.+?).(jpg|png|pdf|gif)\"/i"; // your support filetypes
$mediaFind = preg_match_all($mediaRegex, $content, $media);
if (isset($media[2]) && count($media[2]) > 0)
return $media[2];
return false;
}
function get_library_items() {
// get the media library for comparison
$library = array();
$args = array(
'post_type' => 'attachment',
//'post_mime_type' => 'image', // if theres only one
'numberposts' => -1,
'post_status' => null,
'post_parent' => null,
);
$attachments = get_posts($args);
foreach ($attachments as $post)
$library[$post->ID] = wp_get_attachment_url($post->ID); // or add more info about thumbnails, etc
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment