functions - Frontend Feature image upload not work

admin2025-06-02  2

Tried this code but it's not working:

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');
        }
         if ($_FILES) {
            foreach ($_FILES as $file => $array) {
                if ($_FILES[$file]['error'] !== UPLOAD_ERR_OK) {
                    return "upload error : " . $_FILES[$file]['error'];
                }
                $attach_id = media_handle_upload( $file, $post_id );
            }   
        }
        // Define attachment metadata
       $attach_data = wp_generate_attachment_metadata( $attach_id, $file );
       // Assign metadata to attachment
       wp_update_attachment_metadata( $attach_id, $attach_data );
        if ($attach_id > 0){
            //and if you want to set that image as Post  then use:
            set_post_meta($post_id,'_thumbnail_id',$attach_id);
        }

Tried this code but it's not working:

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');
        }
         if ($_FILES) {
            foreach ($_FILES as $file => $array) {
                if ($_FILES[$file]['error'] !== UPLOAD_ERR_OK) {
                    return "upload error : " . $_FILES[$file]['error'];
                }
                $attach_id = media_handle_upload( $file, $post_id );
            }   
        }
        // Define attachment metadata
       $attach_data = wp_generate_attachment_metadata( $attach_id, $file );
       // Assign metadata to attachment
       wp_update_attachment_metadata( $attach_id, $attach_data );
        if ($attach_id > 0){
            //and if you want to set that image as Post  then use:
            set_post_meta($post_id,'_thumbnail_id',$attach_id);
        }
Share Improve this question edited Feb 13, 2017 at 13:47 吉 宁 4141 gold badge4 silver badges12 bronze badges asked Feb 13, 2017 at 10:37 user3568652user3568652 12 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

Use the following code.

<?php
// Upload image to wordpress media and save image url to custom field.
if(isset($_FILES['images'])) {
    // These files need to be included as dependencies when on the front end.
    require_once( ABSPATH . 'wp-admin/includes/image.php' );
    require_once( ABSPATH . 'wp-admin/includes/file.php' );
    require_once( ABSPATH . 'wp-admin/includes/media.php' );
    require_once( ABSPATH . 'wp-admin/includes/admin.php' );

    $file_return = wp_handle_upload( $_FILES['images'], array('test_form' => false ) );

    if( isset( $file_return['error'] ) || isset( $file_return['upload_error_handler'] ) ) {
        echo $file_return['error'];
    } else {
        $attachment = array(
            'post_mime_type' => $file_return['type'],
            'post_title' => preg_replace( '/\.[^.]+$/', '', basename( $file_return['file'] ) ),
            'post_content' => '',
            'post_status' => 'inherit',
            'guid' => $file_return['url'],
            'post_parent' => $post_id,
        );

        // Insert attachment
        $attachment_id = wp_insert_attachment( $attachment, date('Y/m/') .  basename($file_return['url']) );

        // Set post thumbnail
        if( !has_post_thumbnail($post_id) ) {
            set_post_thumbnail($post_id, $attachment_id);
        }

        // Update post meta
        if( $attachment_id ) {
            $file_location = get_bloginfo('url') . '/app/uploads/' . date('Y/m/') . basename($file_return['url']);
            update_post_meta($attachment_id, '_wp_attachment_metadata', serialize(array($file_location)));
        }
    }
}
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748872431a314422.html

最新回复(0)