Comments (1)
Basically done like in the following example. This was added to a theme.
Template:
<?php
if ( isset( $_REQUEST['error']['upload_error'] ) )
printf( $msg, 'Error: Die Zuweisung Ihres Avatars hat nicht funktioniert' );
?>
<?= get_avatar(
get_current_user_id(),
'192px',
'',
'display_name',
[ 'class' => 'c-image--avatar', ]
); ?>
<input type="file" accept="image/*" size="100" name="user_avatar">
Processing:
if ( ! empty( $_FILES['user_avatar']['name'] ) ) {
$this->uploadAvatar( $user );
}
# …
/**
* Upload an avatar as media file/attachment
*
* Connect the Avatar with the user via user meta
* and the user via post meta to the attachment post.
*
* @param array $user
* @return array $messages
*/
private function uploadAvatar( Array $user )
{
require_once ABSPATH.'/wp-admin/includes/media.php';
require_once ABSPATH.'/wp-admin/includes/file.php';
require_once ABSPATH.'/wp-admin/includes/image.php';
$att_id = media_handle_upload( 'user_avatar', $post_id = - 1 );
if ( is_wp_error( $att_id ) )
$this->redirectPage( $att_id->get_error_code() );
$key = apply_filters( 'wcm.avatar.meta_key', 'user_avatar' );
// Attach attachment ID to user meta as single entry (querying allowed)
$user_meta = update_user_meta( $user['ID'], $key, $att_id );
if ( FALSE === $user_meta )
$this->redirectPage( 'avatar_upload_umeta' );
// Attach user to attachment (single meta entry to allow querying)
$post_meta = add_post_meta( $att_id, 'user_id', $user['ID'], TRUE );
if ( FALSE === $post_meta )
$this->redirectPage( 'avatar_upload_pmeta' );
}
Finally the redirect function
private function redirectPage( $reason )
{
if ( headers_sent() ) {
exit( $reason );
}
$key = $reason === 'saved' ? 'success' : 'error';
$link = get_permalink( get_queried_object() );
$url = $reason === 'login'
? wp_login_url( $link )
: add_query_arg( [ $key => $reason ], $link );
wp_safe_redirect( $url );
exit();
}
from wcm-avatar.
Related Issues (20)
- Add better labels for GitHub
- Default/Fallback avatar HOT 1
- Reconsider positioning of UI on profile page HOT 3
- Disable Multi-Selection HOT 1
- Multi Uploader breaks when Upload fails HOT 3
- Helpers for common checks HOT 4
- Create *.pot file HOT 4
- Use correct text domain
- AvatarScriptsService lacks check for permissions HOT 1
- Reconsider using file_get_contents() in UnderscoreTemplateScripts HOT 11
- Make Plugin work for users without `upload_files` permission HOT 2
- Remove (apparently) unused AdminStylesService HOT 3
- Avatar Settings Page ends up weird HOT 3
- As a user who uploaded a custom avatar, I do not want to get fooled about using Gravatar HOT 6
- Uploader broken HOT 3
- AvatarBrowserUploaderSaveService breaks on non-english locale HOT 2
- AvatarMIMELimitService limits mime types for all uploads, but only for the browser-uploader HOT 3
- AvatarUploadSizeLimit only affects multi-uploader (plupload)
- AvatarBrowserUploaderSaveService nests errors one level to deep HOT 1
- AvatarDimensionLimitService and AvatarExistsLimitService only affect Multi-Uploader
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from wcm-avatar.