Created
February 8, 2018 20:54
-
-
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
This file contains hidden or 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
<?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