Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm using Simple Local Avatars for uploading custom user avatars.

How can I create a frontend page with the avatar upload function?

Below please find the code from Simple Local Avatars plugin:

    <?php
    /**
     * add field to user profiles
     */
     class simple_local_avatars {
        function simple_local_avatars() {
            add_filter('get_avatar', array($this, 'get_avatar'), 10, 5);
            add_action('admin_init', array($this, 'admin_init'));
            add_action('show_user_profile', array($this, 'edit_user_profile'));
            add_action('edit_user_profile', array($this, 'edit_user_profile'));
            add_action('personal_options_update', array($this, 'edit_user_profile_update'));
            add_action('edit_user_profile_update', array($this, 'edit_user_profile_update'));
            add_filter('avatar_defaults', array($this, 'avatar_defaults'));
        }
    function get_avatar($avatar = '', $id_or_email, $size = '80', $default = '', $alt = false) {
        if (is_numeric($id_or_email))
            $user_id = (int) $id_or_email;
        elseif (is_string($id_or_email)) {
            if ($user = get_user_by_email($id_or_email))
                $user_id = $user->ID;
        } elseif (is_object($id_or_email) && !empty($id_or_email->user_id))
            $user_id = (int) $id_or_email->user_id;
        if (!empty($user_id))
            $local_avatars = get_user_meta($user_id, 'simple_local_avatar', true);
        if (!isset($local_avatars) || empty($local_avatars) || !isset($local_avatars['full'])) {
            if (!empty($avatar))
                return "<img src='http://www.wrongmag.ru/wp-content/themes/wrongmag/scripts/timthumb.php?src=/uploads/default-avatar.png&amp;w={$size}&amp;h={$size}&amp;zc=1' class='avatar avatar-{$size} photo' height='{$size}' width='{$size}' />";
            remove_filter('get_avatar', 'get_simple_local_avatar');
            $avatar = get_avatar($id_or_email, $size, $default);
            add_filter('get_avatar', 'get_simple_local_avatar', 10, 5);
            return $avatar;
        }
        if (!is_numeric($size))
            $size = '80';
        if (empty($alt))
            $alt = get_the_author_meta('display_name', $user_id);
        if (empty($local_avatars[$size])) {
            $upload_path = wp_upload_dir();
            $avatar_full_path = str_replace($upload_path['baseurl'], $upload_path['basedir'], $local_avatars['full']);
            $image_sized = image_resize($avatar_full_path, $size, $size, true);
            if (is_wp_error($image_sized))
                $local_avatars[$size] = $local_avatars['full'];
            else
                $local_avatars[$size] = str_replace($upload_path['basedir'], $upload_path['baseurl'], $image_sized);
            update_user_meta($user_id, 'simple_local_avatar', $local_avatars);
        } elseif (substr($local_avatars[$size], 0, 4) != 'http')
            $local_avatars[$size] = site_url($local_avatars[$size]);
        $author_class = is_author($user_id) ? ' current-author' : '';
        $avatar = "<img alt='" . esc_attr($alt) . "' src='" . $local_avatars[$size] . "' class='avatar avatar-{$size}{$author_class} photo' height='{$size}' width='{$size}' />";
        return $avatar;
    }

    function admin_init() {
        load_plugin_textdomain('simple-local-avatars', false, dirname(plugin_basename(__FILE__)) . '/languages/');
        register_setting('discussion', 'simple_local_avatars_caps', array($this, 'sanitize_options'));
        add_settings_field('simple-local-avatars-caps', __('Local Avatar Permissions', 'simple-local-avatars'), array($this, 'avatar_settings_field'), 'discussion', 'avatars');
    }

    function sanitize_options($input) {
        $new_input['simple_local_avatars_caps'] = empty($input['simple_local_avatars_caps']) ? 0 : 1;
        return $new_input;
    }

    function avatar_settings_field($args) {
        $options = get_option('simple_local_avatars_caps');
        echo '<label for="simple_local_avatars_caps">
            <input type="checkbox" name="simple_local_avatars_caps" id="simple_local_avatars_caps" value="1" ' . @checked($options['simple_local_avatars_caps'], 1, false) . ' />
            ' . __('Only allow users with file upload capabilities to upload local avatars (Authors and above)', 'simple-local-avatars') . '
        </label>';
    }

    function edit_user_profile($profileuser) {
        ?>
        <h3><?php _e('Avatar', 'simple-local-avatars'); ?></h3>
        <table class="form-table">
            <tr>
                <th><label for="simple-local-avatar"><?php _e('Upload Avatar', 'simple-local-avatars'); ?></label></th>
                <td style="width: 50px;" valign="top">
                    <?php echo get_avatar($profileuser->ID); ?>
                </td>
                <td>
                    <?php
                    $options = get_option('simple_local_avatars_caps');
                    if (empty($options['simple_local_avatars_caps']) || current_user_can('upload_files')) {
                        do_action('simple_local_avatar_notices');
                        wp_nonce_field('simple_local_avatar_nonce', '_simple_local_avatar_nonce', false);
                    ?>
                    <input type="file" name="simple-local-avatar" id="simple-local-avatar" /><br />
                    <?php
                    if (empty($profileuser->simple_local_avatar))
                        echo '<span class="description">' . __('No local avatar is set. Use the upload field to add a local avatar.', 'simple-local-avatars') . '</span>';
                    else
                        echo '<input type="checkbox" name="simple-local-avatar-erase" value="1" /> ' . __('Delete local avatar', 'simple-local-avatars') . '<br />
                            <span class="description">' . __('Replace the local avatar by uploading a new avatar, or erase the local avatar (falling back to a gravatar) by checking the delete option.', 'simple-local-avatars') . '</span>';
                    } else {
                        if (empty($profileuser->simple_local_avatar))
                            echo '<span class="description">' . __('No local avatar is set. Set up your avatar at Gravatar.com.', 'simple-local-avatars') . '</span>';
                        else
                            echo '<span class="description">' . __('You do not have media management permissions. To change your local avatar, contact the blog administrator.', 'simple-local-avatars') . '</span>';
                    }
                    ?>
                </td>
            </tr>
        </table>
        <script type="text/javascript">var form=document.getElementById('your-profile');form.encoding='multipart/form-data';form.setAttribute('enctype','multipart/form-data');</script>
        <?php
    }

    function edit_user_profile_update($user_id) {
        if (!wp_verify_nonce($_POST['_simple_local_avatar_nonce'], 'simple_local_avatar_nonce'))
            return;
        if (!empty($_FILES['simple-local-avatar']['name'])) {
            $mimes = array(
                'jpg|jpeg|jpe' => 'image/jpeg',
                'gif' => 'image/gif',
                'png' => 'image/png',
                'bmp' => 'image/bmp',
                'tif|tiff' => 'image/tiff'
            );
            $avatar = wp_handle_upload($_FILES['simple-local-avatar'], array('mimes' => $mimes, 'test_form' => false));
            if (empty($avatar['file'])) {
                switch ($avatar['error']) {
                    case 'File type does not meet security guidelines. Try another.':
                        add_action('user_profile_update_errors', create_function('$a', '$a->add("avatar_error",__("Please upload a valid image file for the avatar.","simple-local-avatars"));'));
                        break;
                    default:
                        add_action('user_profile_update_errors', create_function('$a', '$a->add("avatar_error","<strong>".__("There was an error uploading the avatar:","simple-local-avatars")."</strong> ' . esc_attr($avatar['error']) . '");'));
                }
                return;
            }
            $this->avatar_delete($user_id);
            update_user_meta($user_id, 'simple_local_avatar', array('full' => $avatar['url']));
        } elseif (isset($_POST['simple-local-avatar-erase']) && $_POST['simple-local-avatar-erase'] == 1)
            $this->avatar_delete($user_id);
    }

    function avatar_defaults($avatar_defaults) {
        remove_action('get_avatar', array($this, 'get_avatar'));
        return $avatar_defaults;
    }

    function avatar_delete($user_id) {
        $old_avatars = get_user_meta($user_id, 'simple_local_avatar', true);
        $upload_path = wp_upload_dir();
        if (is_array($old_avatars)) {
            foreach ($old_avatars as $old_avatar) {
                $old_avatar_path = str_replace($upload_path['baseurl'], $upload_path['basedir'], $old_avatar);
                @unlink($old_avatar_path);
            }
        }
        delete_user_meta($user_id, 'simple_local_avatar');
    }
}
$simple_local_avatars = new simple_local_avatars;

if (!function_exists('get_simple_local_avatar')):
    function get_simple_local_avatar($id_or_email, $size = '80', $default = '', $alt = false) {
        global $simple_local_avatars;
        return $simple_local_avatars->get_avatar('', $id_or_email, $size, $default, $alt);
    }
endif;

register_uninstall_hook(__FILE__, 'simple_local_avatars_uninstall');

function simple_local_avatars_uninstall() {
    $simple_local_avatars = new simple_local_avatars;
    $users = get_users_of_blog();
    foreach ($users as $user)
        $simple_local_avatars->avatar_delete($user->user_id);
    delete_option('simple_local_avatars_caps');
}

I have tried many variants but all of them failed.

Could you please give a hint, what pieces of code I shall copy to my custom frontend template file?

Thank you very much.

share|improve this question
add comment

4 Answers

up vote 2 down vote accepted

Sergey, use this in place where you are going to put form to edit avatar:

<?php
$myAv = new simple_local_avatars();
$myAv->edit_user_profile($profileuser); 
?>
share|improve this answer
2  
Please stick to using English, so all users of site can benefit from your answers. –  Rarst Aug 30 '11 at 8:54
add comment
<?php get_header();
global $user_ID;
if ($user_ID) {
$user_info = get_userdata($user_ID);
$id = $user_info->ID;
}
 ?>
<?php if(isset($_POST['user_avatar_edit_submit']))
      {
           do_action('edit_user_profile_update', $id);
      }
?>

<form id="your-profile" action="" method="post">
    <?php
    $myAv = new simple_local_avatars();
    $myAv->edit_user_profile($user_info);
    ?>
    <input type="submit" name="user_avatar_edit_submit" value="OK"/>
</form>



<?php get_footer(); ?>
share|improve this answer
    
Explaining what's going on in your sample code would really improve the quality of your answer. Be it a brief introduction, be it in form of code comments. –  brasofilo Nov 19 '12 at 16:42
add comment

After hours I finally figured it out. We need second parameters passed to the actions:

  1. action 'edit_user_profile' needs a Wordpress user object (we take it from WP_User class)
  2. action 'edit_user_profile_update' needs the user id

The first action is rendering the upload field and the current avatar and the second one is doing the upload itself. That's why we need to trigger it after clicking the button. We listen for the button in the beginning of the code, so after submitting the page refreshes and the avatar is updated (otherwise it shows up as a broken image).

Here is my setup:

<?php if(isset($_POST['user_avatar_edit_submit']))
      {
           do_action('edit_user_profile_update', bbp_get_displayed_user_id());
      }
?>
<form id="your-profile" action="<?php bbp_user_profile_edit_url( bbp_get_displayed_user_id() ); ?>" method="post">
<?php 
    $this_user = new WP_User(bbp_get_displayed_user_id());
    do_action('edit_user_profile', $this_user);
?>

    <input type="submit" name="user_avatar_edit_submit" value="OK"/>
</form>

I hope that will help other guys in future and save them that headache.

share|improve this answer
2  
Are you the guy who originally asked the question? –  kaiser Sep 14 '12 at 2:23
add comment

add

do_action('show_user_profile');
do_action('edit_user_profile');

to your custom frontend template

and when saving add :

do_action('personal_options_update');
do_action('edit_user_profile_update');
share|improve this answer
    
OK, thank you. But what about the rest of the code? What piece of code shall i put between the two snippets you gave? –  Sergey Sedykh Aug 4 '11 at 9:30
    
hmmm, your custom frontend template, take a look at wordpress.stackexchange.com/questions/9775/… –  Bainternet Aug 4 '11 at 9:34
    
Thanks for the link, but all i need is avatar upload file input. Can you point which piece of Simple Avatar Uploads shall i use between the two code snippets? –  Sergey Sedykh Aug 4 '11 at 9:51
    
Still can't get it done. Any hints? –  Sergey Sedykh Aug 15 '11 at 9:24
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.