How do i upload an image and return the image id?

Take a look at side loading images. media_sideload_image()/wp_handle_sideload() and get the ID from the URL. attachment_url_to_postid. <?php $url = “http://wordpress.org/about/images/logos/wordpress-logo-stacked-rgb.png”; $title = “Some Image Title”; $alt_text = “Some Alt Text”; require_once(ABSPATH . ‘wp-admin/includes/media.php’); require_once(ABSPATH . ‘wp-admin/includes/file.php’); require_once(ABSPATH . ‘wp-admin/includes/image.php’); // sideload the image — requires the files above to work correctly $src = media_sideload_image( $url, … Read more

how to test for attached image

function has_image_attachment($post_id) { $args = array( ‘post_type’ => ‘attachment’, ‘post_mime_type’ => ‘image/jpeg’, ‘numberposts’ => -1, ‘post_status’ => null, ‘post_parent’ => $post_id ); $attachments = get_posts($args); if(is_array($attachments) && count($attachments) > 0) { //Has image attachments return true; } else { return false; } }

Creating a multi-file upload form on the front end for attachments

This may not be the most elegant way to do it (I’m not sure if overwriting the $_FILES global is even allowed) but this seems to work: global $post; if ($_FILES) { $files = $_FILES[‘upload_attachment’]; foreach ($files[‘name’] as $key => $value) { if ($files[‘name’][$key]) { $file = array( ‘name’ => $files[‘name’][$key], ‘type’ => $files[‘type’][$key], ‘tmp_name’ … Read more

Moving image attachment from post to another?

As far as I know, you’d have to use a combination of built-in functions to achieve this. If there is a plugin, you’d have to research for “Bulk parent” or “Bulk re-attachment”. Unless you have a logic connecting those posts, I think you’d have to do it post by post. Be it with PHPMyAdmin (changing … Read more

Add column for attachment file size

Working code: // Add custom column with file size info to attachment page add_filter( ‘manage_media_columns’, ‘bb2_manage_media_columns’, 10, 2 ); function bb2_manage_media_columns( $columns ) { $columns[‘filesize’] = __( ‘Storlek’, ‘bb2’ ); return $columns; } add_action( ‘manage_media_custom_column’, ‘bb2_manage_media_custom_column’, 10, 2 ); function bb2_manage_media_custom_column( $column_name, $id ) { switch ( $column_name ) { case ‘filesize’ : $bytes = … Read more

Programatically creating image attachments from local URLs and setting featured image

Answers: Is post_guid the image location reference? Or is the path to the image stored somewhere else? $post->guid is the record in a post which holds the URL for your attachment. Where is featured image set? featured image is saved as post meta so use update_post_meta() once you have the attachment id: update_post_meta( $post->ID, ‘_thumbnail_id’, … Read more