Upload image to wordpress

时间:2013-05-06 作者:Monogot

我是一个初级网站,使用WordPress,我正在编写涉及个人资料页面的插件(不是avatar,因为我实现了这个字段,用户可以上传图像,任何东西,比如我朋友的照片),我添加了字段上传,从目录浏览文件,并使用WordPress将其保存到数据库wp_handle_upload. 我运行了我的代码,但图片没有显示我不知道我的代码(下面)的哪一部分是错误的。

我应该怎么做才能解决这个问题!

这是我修改的代码this discussion

<?php
add_action( \'user_edit_form_tag\',\'make_uploadable_form\');
function make_uploadable_form() {
echo \' enctype="multipart/form-data"\';
}
?>
==================编辑=======================
以下是HTML代码

<?php
add_action( \'show_user_profile\', \'UploadField\' );
add_action( \'edit_user_profile\', \'UploadField\' );

function UploadField( $user ) { 
if ( !current_user_can( \'edit_user\') )
    return false;
$prfile_image_id = get_user_meta($user->ID,\'profile_photo\',true);
$profile_image = wp_get_attachment_image($profile_image_id,"full");

if(!empty($profile_image)){
    echo $profile_image;
}
else{
?>

<h3>Extra profile information</h3>

<table class="form-table">
    <tr>
        <th><label for="Upload">Upload</label></th>
            <td>
                <input name="profile_photo" type="file" id="profile_photo" value="" />
            </td>
</table>

<?php
}
以下是句柄文件上载:

add_action( \'personal_options_update\', \'save_user_custom\' );
add_action( \'edit_user_profile_update\', \'save_user_custom\' );

function save_user_custom($user_id){

if($user_id == false)
    return false;

// If the upload field has a file in it
if(isset($_FILES[\'profile_photo\'])){
    if(!function_exists(\'wp_handle_upload\'))
        require_once(ABSPATH.\'wp-admin/includes/file.php\');

    // Get the type of the uploaded file. This is returned as "type/extension"
    $arr_file_type = wp_check_filetype(basename($_FILES[\'profile_photo\'][\'name\']));
    $uploaded_file_type = $arr_file_type[\'type\'];
    // Set an array containing a list of acceptable formats
    $allowed_file_types = array(\'image/jpg\',\'image/jpeg\',\'image/gif\',\'image/png\');
    // If the uploaded file is the right format
    if(in_array($uploaded_file_type, $allowed_file_types)) {
        // Options array for the wp_handle_upload function. \'test_upload\' => false
        $upload_overrides = array( \'test_form\' => false ); 

        // Handle the upload using WP\'s wp_handle_upload function. Takes the posted file and an options array
        $uploaded_file = wp_handle_upload($_FILES[\'profile_photo\'], $upload_overrides);

        // If the wp_handle_upload call returned a local path for the image
        if(isset($uploaded_file[\'file\'])) {

            // The wp_insert_attachment function needs the literal system path, which was passed back from wp_handle_upload
            $file_name_and_location = $uploaded_file[\'file\'];

            // Generate a title for the image that\'ll be used in the media library
            $file_title_for_media_library = \'your title here\';

            // Set up options array to add this file as an attachment
            $attachment = array(
                \'post_mime_type\' => $uploaded_file_type,
                \'post_title\' => \'Uploaded image \' . addslashes($file_title_for_media_library),
                \'post_content\' => \'\',
                \'post_status\' => \'inherit\'
            );

            // Run the wp_insert_attachment function. This adds the file to the media library and generates the thumbnails. If you wanted to attch this image to a post, you could pass the post id as a third param and it\'d magically happen.
            $attach_id = wp_insert_attachment( $attachment, $file_name_and_location );
            require_once(ABSPATH . "wp-admin" . \'/includes/image.php\');
            $attach_data = wp_generate_attachment_metadata( $attach_id, $file_name_and_location );
            wp_update_attachment_metadata($attach_id,  $attach_data);

            update_user_meta( $user_id, \'profile_photo\', $attach_id );
        } 

    }

}
}
?>

1 个回复
最合适的回答,由SO网友:Ralf912 整理而成

Output during saving

在数据保存期间,您不能打印某些内容。如果您使用die( var_dump( $your_var ) ); 它将打印值,但也会停止脚本。原因是,首先将解析数据。然后您将被重定向回profile.php. 因此,在解析数据时打印的所有内容都将在重定向时丢失。

您必须获取用户元并检查图像是否保存在用户元中。

function UploadField( $user ) {
  if ( ! current_user_can( \'edit_user\' ) )
    return false;

$profile_image_id = get_user_meta( $user->ID, \'profile_photo\', true );
$profile_image = wp_get_attachment_image( $profile_image_id );

if ( ! empty( $profile_image ) )
//display image
echo $profile_image;
// else 
// and/or display form for uplaod image

Some issues with the code

if ( ! current_user_can( \'edit_user\' ) )
  return false;
current_user_can() 只需要一个参数。

update_user_meta( $user_id, \'profile_photo\', $_POST[\'profile_photo\'] );
$_POST 是超全局的,它不包含所需的信息。代替var_dump( $uploaded_file ); 具有update_user_meta( $user_id, \'profile_photo\', $attach_id ); (或更换$attach_id 使用要存储的数据)

结束

相关推荐

Upload images - Theme options

我正在使用这个很棒的[图坦卡门+教程][1]创建主题选项页面。这对我来说很好,因为我需要通过css更改div的背景图像。它可以很好地与一个图像,但我不知道如何使它与2个图像?如果你能告诉我我做错了什么,那将是一个巨大的帮助?谢谢 <?php function wptuts_get_default_options() { $options = array(); $options[] = array(\'logo\' => \'\');