media_handle_upload : undefined function?

So I dont know if its the best way but my problem was the if statement surrounding the require_once on the necessary files that contain the function. Commented out the IF statement and it did the trick. I’ll go have that coffee now 😉 //if (!function_exists(‘wp_generate_attachment_metadata’)){ require_once(ABSPATH . ‘wp-admin/includes/image.php’); require_once(ABSPATH . ‘wp-admin/includes/file.php’); require_once(ABSPATH . ‘wp-admin/includes/media.php’); … Read more

Is there a way to get attachment data?

Don’t forget that attachments are just posts (though they are native posts and those tend to be a special case). Thus simply get_post() on attachment ID should get you typical post object for it. 🙂 Similarly all API functions that work on posts should work on attachments.

wp.media edit attachment screen

Long answer: Peeking into wp-includes/js/media-models.js, I see this: if ( ‘select’ === attributes.frame && MediaFrame.Select ) { frame = new MediaFrame.Select( attributes ); } else if ( ‘post’ === attributes.frame && MediaFrame.Post ) { frame = new MediaFrame.Post( attributes ); } else if ( ‘manage’ === attributes.frame && MediaFrame.Manage ) { frame = new MediaFrame.Manage( … Read more

Why does SVG upload in Media Library fail if the file does not have an XML tag at the beginning?

It seems that in the recent releases of WordPress, changes were made to the mime type handling to make sure that files have the extension they say they do: https://make.wordpress.org/core/2018/12/13/backwards-compatibility-breaks-in-5-0-1/ This poses an issue for SVG files without the tag in them. SVG is actually an XML, and WordPress is now requiring to have a … Read more

Easy way to delete 70k posts and attached media?

If you know how to find the posts, then we can use WP CLI to do a 2 step process. First, grab all the post IDs in those categories using wp post list E.g. posts=$(wp post list –field=”ID” –category__in=”1,2,etc”) wp post delete $posts –force Then we can plug the values in posts into wp post … Read more

Get image alt attribute just by image URL

Using the function found here, you could add this to your functions.php // retrieves the attachment ID from the file URL function pippin_get_image_id($image_url) { global $wpdb; $attachment = $wpdb->get_col($wpdb->prepare(“SELECT ID FROM $wpdb->posts WHERE guid=’%s’;”, $image_url )); return $attachment[0]; } But to use it, you need the full image URL. $img_url = get_bloginfo(‘url’) . “https://wordpress.stackexchange.com/bc/wp-content/uploads/placeholder-image.png”; if … Read more

Getting alt text of featured image

The alt text is stored as post meta on the attachment, and can be retrieved with get_post_meta() combined with get_post_thumbnail_id(): <?php echo get_post_meta( get_post_thumbnail_id(), ‘_wp_attachment_image_alt’, true ); ?> But if you just use the the_post_thumbnail() function then you don’t need to bother, the img tag will be output for you, with the alt text: <?php … Read more