上传到前端的图像可以正确显示,但不能在媒体编辑器中显示。

时间:2017-10-09 作者:lukgoh

        if ($_FILES) {

            $uploaddir = wp_upload_dir();

            $file = $_FILES[featured_image];

            $uploadfile = $uploaddir[\'path\'] . \'/\' . basename( $file[\'name\'] );

            move_uploaded_file( $file[\'tmp_name\'] , $uploadfile );

            $filename = basename( $uploadfile );

            $wp_filetype = wp_check_filetype(basename($filename), null );

            $attachment = array(
                \'post_mime_type\' => $wp_filetype[\'type\'],
                \'post_title\'     => preg_replace(\'/\\.[^.]+$/\', \'\', $filename),
                \'post_content\'   => \'\',
                \'post_status\'    => \'inherit\',
                \'menu_order\'     => $_i + 1000
            );

            $attach_id = wp_insert_attachment( $attachment, $uploadfile );

            // Make sure that this file is included, as wp_generate_attachment_metadata() depends on it.
            require_once(ABSPATH . "wp-admin" . \'/includes/image.php\');
            require_once(ABSPATH . "wp-admin" . \'/includes/file.php\');
            require_once(ABSPATH . "wp-admin" . \'/includes/media.php\');

            // Generate the metadata for the attachment, and update the database record.
            $attach_data = wp_generate_attachment_metadata( $attach_id, $filename );
            wp_update_attachment_metadata( $attach_id, $attach_data );

            set_post_thumbnail( $new_job_id, $attach_id );

        }
图像上传正确,并设置为所选帖子的特色图像。图像的显示也与普通图像一样(除了只能显示完整的图像大小,不能加载缩略图大小。)但在后端的媒体库中,图像显示为带有折叠角的页面,您无法看到实际图像。

我做错了什么?

2 个回复
最合适的回答,由SO网友:Tom J Nowell 整理而成

看来你已经走上了冗长的路线,手动完成了上传的每一步。

但是,为什么不使用为您完成这一切的侧加载功能呢?您的大部分代码可以替换为:

// 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\' );

$attach_id = media_handle_upload( \'featured_image\', $new_job_id );
if ( is_wp_error( $attach_id ) ) {
    // There was an error uploading the image.
} else {
    // The image was uploaded successfully!
}
从那里你可以设置你的帖子缩略图等,查阅文档media_handle_upload 如需了解更多详细信息,请记住向上载程序添加临时检查,否则您的网站可能会遭到黑客攻击

SO网友:lukgoh
        if ($_FILES) {

            // Make sure that this file is included, as wp_generate_attachment_metadata() depends on it.
            require_once(ABSPATH . "wp-admin" . \'/includes/image.php\');
            require_once(ABSPATH . "wp-admin" . \'/includes/file.php\');
            require_once(ABSPATH . "wp-admin" . \'/includes/media.php\');

            $uploaddir = wp_upload_dir();

            $file = $_FILES[featured_image];

            $file_return = wp_handle_upload( $file, array(\'action\' => \'tm_add_new_job\' ) );

            if( isset( $file_return[\'error\'] ) || isset( $file_return[\'upload_error_handler\'] ) ) {

              return false;

            } else {

                $filename = $file_return[\'file\'];

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

                $attachment_id = wp_insert_attachment( $attachment, $file_return[\'url\'] );

                // Generate the metadata for the attachment, and update the database record.
                $attachment_data = wp_generate_attachment_metadata( $attachment_id, $filename );
                wp_update_attachment_metadata( $attachment_id, $attachment_data );

                set_post_thumbnail( $new_job_id, $attachment_id );

            }

        }
结束

相关推荐

Images are not shown

我可以在媒体/图书馆中看到我的图片,但不能在帖子中看到。问题似乎是图像的真实url以图像结尾。jpg(如果我转到这个url,我可以看到图像)。但在帖子中,我看到了“断开的图像链接”图标。当我检查图像url时,它以图像sizeXsize结尾。jpg,所以Wordpress出于某种原因添加了sizeXsize(例如1000x1000)。如何解决此问题?编辑1:我刚刚安装了WP。我试过theams 2111和215,同样的问题。我已经安装了很多插件,请看一下。Edit2:我已经停用了所有插件,仍然是同一个问题。