0


I managed to add an upload field to the user registration form of the website I'm building. I, also, was able to handle the upload.
However, despite the file was correctly copied to the uploads folder, the Media section of the Admin Dashboard is still empty.
Here is the code so far:

function handle_upload($user_id){
    if ( ! function_exists( 'wp_handle_upload' ) ) {
        require_once( ABSPATH . 'wp-admin/includes/image.php' );
        require_once( ABSPATH . 'wp-admin/includes/file.php' );
        require_once( ABSPATH . 'wp-admin/includes/media.php' );
    }

    $uploadedfile = $_FILES['logo'];

    $upload_overrides = array( 'test_form' => false );

    $movefile = wp_handle_upload( $uploadedfile, $upload_overrides );

    if ( $movefile && ! isset( $movefile['error'] ) ) {
        echo "File is valid, and was successfully uploaded.\n";
        add_user_meta( $user_id, 'logo', $movefile['url']);
    } else {
        /**
         * Error generated by _wp_handle_upload()
         */
        echo $movefile['error'];
    }
}

How can I fix it? How can I associate the user_id to the newly attached file? Thanks!

UPDATE

I modified the function, by adding the wp_insert_attachment function:

function handle_upload($user_id){
    if ( ! function_exists( 'wp_handle_upload' ) ) {
        require_once( ABSPATH . 'wp-admin/includes/image.php' );
        require_once( ABSPATH . 'wp-admin/includes/file.php' );
        require_once( ABSPATH . 'wp-admin/includes/media.php' );
    }

    $uploadedfile = $_FILES['logo'];

    $upload_overrides = array( 'test_form' => false );

    $movefile = wp_handle_upload( $uploadedfile, $upload_overrides );

    // // Prepare an array of post data for the attachment.
    $attachment = array(
        'guid'           => $movefile['url'], 
        'post_mime_type' => $uploadedfile['type'],
        'post_title'     => $uploadedfile['name'],
        'post_content'   => '',
        'post_status'    => 'inherit'
    );

    // Insert the attachment.
    $attach_id = wp_insert_attachment( $attachment, $movefile['url'], $user_id );

    if ( $movefile && ! isset( $movefile['error'] ) ) {
        echo "File is valid, and was successfully uploaded.\n";
        add_user_meta( $user_id, 'logo', $movefile['url']);
    } else {
        /**
         * Error generated by _wp_handle_upload()
         */
        echo $movefile['error'];
    }
}

But I get this error message: WordPress database error: [Cannot add or update a child row: a foreign key constraint fails (mydb.wp_posts, CONSTRAINT author_id FOREIGN KEY (post_author) REFERENCES wp_users (ID) ON DELETE CASCADE ON UPDATE CASCADE)]

Do I have to specify a post_id?

  • 1
    it's the normal function of wp_handle_upload. to add the file in the medias, use codex.wordpress.org/Function_Reference/media_handle_upload – mmm Feb 21 '18 at 14:32
  • Thanks for the answers, but those functions allow to link a post to an attachment. Is there a function that allows to link that file to a user? Thanks – DamianFox Feb 21 '18 at 14:54
  • The only association files have with users in WordPress is the author. You can set the author of an upload by setting the post_author in the $post_data argument for media_handle_upload. Keep in mind that in WordPress code media in the media library is referred to as 'attachments', but they don't need to be attached to posts. – Jacob Peattie Feb 21 '18 at 14:58
  • I updated the question. – DamianFox Feb 22 '18 at 15:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.