Skip to content

Instantly share code, notes, and snippets.

@trepmal
Created June 1, 2012 20:32
Show Gist options
  • Save trepmal/2854981 to your computer and use it in GitHub Desktop.
Save trepmal/2854981 to your computer and use it in GitHub Desktop.
User Meta Upload
<?php
/*
Plugin Name: User Meta Upload
Plugin URI: http://trepmal.com
Description: Add a file-upload form to the user edit profile page
Version: 0.1
Author: Kailey Lampert
Author URI: http://kaileylampert.com
Copyright (C) 2012 Kailey Lampert
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
add_action( 'personal_options_update', 'save_custom_profile_fields' );
add_action( 'edit_user_profile_update', 'save_custom_profile_fields' );
function save_custom_profile_fields( $user_id ) {
//print_r($_FILES);
$_POST['action'] = 'wp_handle_upload';
$r = wp_handle_upload( $_FILES['picture'] );
update_user_meta( $user_id, 'picture', $r, get_user_meta( $user_id, 'picture', true ) );
//take upload, add to media library
if (isset($r['file'])) {
$filename = $r['file'];
$title = explode('.', basename( $filename ) );
$ext = array_pop( $title );
$attachment = array(
'guid' => $r['url'],
'post_mime_type' => $r['type'],
'post_title' => implode('.', $title ),
'post_content' => '',
'post_status' => 'inherit'
);
$attach_id = wp_insert_attachment( $attachment, $filename );
$attach_data = wp_generate_attachment_metadata( $attach_id, $filename );
wp_update_attachment_metadata( $attach_id, $attach_data );
}
}
add_action( 'personal_options', 'add_profile_options');
function add_profile_options( $profileuser ) {
$r = get_user_meta( $profileuser->ID, 'picture', true );
?>
<tr>
<th scope="row">Picture</th>
<td><input type="file" name="picture" value="" />
<?php // print_r($r);
if (!isset($r['error'])) {
$r = $r['url'];
echo "<img src='$r' />";
} else {
$r = $r['error'];
echo $r;
}
?>
</td>
</tr>
<?php
}
add_action('user_edit_form_tag', 'make_form_accept_uploads');
function make_form_accept_uploads() {
echo ' enctype="multipart/form-data"';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment