Giter Site home page Giter Site logo

Comments (1)

franz-josef-kaiser avatar franz-josef-kaiser commented on August 11, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.