前端功能图片上传不起作用

时间:2017-02-13 作者:user3568652

已尝试此代码,但不起作用:

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);
        }

1 个回复
SO网友:Mostafa Soufi

使用以下代码。

<?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)));
        }
    }
}

相关推荐

functions php file

好的,我正在编辑我的函数。Wordpress中的php文件(关于我的子主题),我删除了一行代码(在删除之前我复制了代码)。这导致了白色的死亡屏幕,所以我简单地粘贴回我删除的代码(是的,我将它粘贴回我删除它的位置),但我仍然有白色的死亡屏幕。我已经完成了这些功能。我上周完成的一个备份中的php文件(功能齐全),并通过FTP将其上载到正确的文件夹,但没有任何效果。我已经尝试再次激活父主题,并创建了另一个子主题,以便比较这两个功能。php文件(将全新的、有效的子主题与不再工作的原始子主题进行比较),代码看起来是