我如何在管理页面中上传图片?

时间:2017-10-21 作者:Tarek

我正在尝试在用户配置文件管理页面上载图像我已找到该功能media_handle_upload 在这里的抄本里,但有点不对劲,我一直error Uploading. 我不知道代码中是否缺少一些东西。

我也尝试在用户编辑页面中使用相同的功能,但我不断收到“无效用户ID”错误。

function image_up_gall(){
?>
<form id="featured_upload" method="post" action="#" enctype="multipart/form-data">
    <input type="file" name="my_image_upload" id="my_image_upload"  multiple="false" />
    <input type="hidden" name="post_id" id="post_id" value="1" />
    <?php wp_nonce_field( \'my_image_upload\', \'my_image_upload_nonce\' ); ?>
    <input id="submit_my_image_upload" name="submit_my_image_upload" type="submit" value="Upload" />
</form>
<?php
// Check that the nonce is valid, and the user can edit this post.
if ( 
    isset( $_POST[\'my_image_upload_nonce\'], $_POST[\'post_id\'] ) 
    && wp_verify_nonce( $_POST[\'my_image_upload_nonce\'], \'my_image_upload\' )
    && current_user_can( \'edit_post\', $_POST[\'post_id\'] )
) {
    // The nonce was valid and the user has the capabilities, it is safe to continue.

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

    // Let WordPress handle the upload.
    // Remember, \'my_image_upload\' is the name of our file input in our form above.
    $attachment_id = media_handle_upload( \'my_image_upload\', $_POST[\'post_id\'] );

    if ( is_wp_error( $attachment_id ) ) {
        // There was an error uploading the image.
        echo "error Uploading";
    } else {
        // The image was uploaded successfully!
        echo "Successfully Uploaded";
    }

} else {

    // The security check failed, maybe show the user an error.
    echo "security check error";
}
}
add_action(\'edit_user_profile\', \'image_up_gall\');
add_action(\'show_user_profile\', \'image_up_gall\');

1 个回复
SO网友:Misha Rudrastyh

首先edit_user_profileshow_user_profile 动作挂钩不必保存图像,只需在那里添加一个字段即可。所以

function image_up_gall(){
?>

    Select image to upload:
    <input type="file" name="fileToUpload" id="fileToUpload">
    <input type="submit" value="Upload Image" name="submit">
 <?php
}
add_action(\'edit_user_profile\', \'image_up_gall\');
add_action(\'show_user_profile\', \'image_up_gall\');
这是因为WordPress已经有了自己的表单标签,只要确保它有enctype="multipart/form-data"

第二步,使用personal_options_updateedit_user_profile_update 您可以保存表单/上载imag,为此,请使用以下代码:

function save_profile_fields( $user_id ) {
$target_dir = "uploads/"; // I recommend to use wp_upload_dir() to get the correct path
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image 
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
            // here the image is uploaded and we can save it to user profile with:
            update_usermeta( $user_id, \'profile_pic\', $target_file );
    }
}

add_action( \'personal_options_update\', \'save_profile_fields\' );
add_action( \'edit_user_profile_update\', \'save_profile_fields\' );
但我建议您使用WordPress默认媒体库来实现这一点,因为有很多代码,所以我最好给您一个指向教程的链接:https://rudrastyh.com/wordpress/customizable-media-uploader.html

结束