$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>functions - Frontend Feature image upload not work|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

functions - Frontend Feature image upload not work

matteradmin9PV0评论

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)));
        }
    }
}
Post a comment

comment list (0)

  1. No comments so far