image upload and get attachment id in wordpress

/* this function for upload image in wordpress and return attchement id code by joshi yogesh ([email protected])*/
function cst_image_upload($img){

    require_once( ABSPATH . 'wp-admin/includes/image.php' );
    $attes_id = array();
    $cnt = count($img['name']);
    for($i=0;$i<$cnt;$i++)
    {
            $name = $img['name'][$i];
            $type = $img['type'][$i];
            $tmp_name = $img['tmp_name'][$i];
            $error = $img['error'][$i];
            $size = $img['size'][$i] ;
        
        $upload_data = array(
            'name'      => $name,
            'type'      => $type,
            'tmp_name'  => $tmp_name,
            'error'     => $error,
            'size'      => $size
            );
        $uploaded_file = wp_handle_upload($upload_data, array('test_form' => false));
        // print_r($uploaded_file);

        if (isset($uploaded_file['file'])) {
            $file_loc   =   $uploaded_file['file'];
            $file_name  =   basename($upload_data['name']);
            $file_type  =   wp_check_filetype($file_name);

            $attachment = array(
                'post_mime_type'    => $file_type['type'],
                'post_title'        => preg_replace('/\.[^.]+$/', '', basename($file_name)),
                'post_content'      => '',
                'post_status'       => 'inherit'
            );

            $attach_id      =   wp_insert_attachment($attachment, $file_loc);
            $attach_data    =   wp_generate_attachment_metadata($attach_id, $file_loc);
            wp_update_attachment_metadata($attach_id, $attach_data);
            array_push($attes_id,$attach_id);
        }
    }
    return $attes_id;
}

Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
wordpress get image by attachment id get attachment page url of images wordpress wordpress acf get image upload id how to get image url from id of uploaded in media wordpress find uploades images ids wordpress php wordpress upload image under post id wordpress functions return ids of attachment image wp get attachment images ids get ids of images attachment of post type wordpress get ids of attachment images wordpress get id of attachment images wordpress get id of attachment image wordpress Wordpress URL thumbnail attachment get get attachment image by post id wordpress get attachment specific thumbnail url in wordpress wordpress get attachment id from image wordpress get attach image by id wordpress get attachment image get image by attachment id wordpress wordpress image attachment object php details wordpress image attachment php details wordpress image attachment details upload image and get id wordpress programmatically wordpress get attachment id from gallery wordpress get attachments id from gallery wordpress wp_get_attachment_image wp_get_attachment_image by id in php upload image and get attachment id wordpress image upload and get id function wordpres get image url from attachment id wordpress wp get image attachment_id wp get image attachment id wordpress get attachment image url how can I get attachment id for wordpress uploaded images? get attachment image wordpress get image url by attachment id wordpress wp get attachment image src by post id wordpress upload ads.txt wordpress plugin file upload and email attachment form wordpress get attachment image id wordpress get image url from attachment id wp_get_attachment_image id how to get image attachment id in wordpress wp get attachment image id wordpress get thumbnail url by attachment id wp_get_attachment_image( get post attachment image in wordpress echo wvg_get_gallery_image_html( $attachment_id, array( wordpress get image by id wordpress display image by id get image attachment by id wp get image get image url using attachment id wordpress wp get attachment image image upload and get attachment id in wordpress
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source