WordPress.org

Ready to get started?Download WordPress

Forums

Simple Local Avatars
Front Side Support (5 posts)

  1. Koff
    Member
    Posted 1 year ago #

    Hej,

    I just wanted to share some code that can be helpful for others to allow adding front side edit option with a shortcode.

    function editavatar_func($atts) {
         extract(shortcode_atts(array(
    	      'size' => '250'
         ), $atts));
          global $user_ID;
    	if ($user_ID) {
    	$user_info = get_userdata($user_ID);
    	$id = $user_info->ID;
    	}
    	 if(isset($_POST['user_avatar_edit_submit']))
          {
    
                 do_action('edit_user_profile_update', $id);
          }
    
            ob_start();
    	wp_nonce_field( 'simple_local_avatar_nonce', '_simple_local_avatar_nonce', false );
            $nonce = ob_get_contents();
            ob_end_clean();
    
           $output = '<form id="your-profile" action="" method="post">';
            $output .=$nonce ;
           $output .=' <div class="avatar-wrap">';
            $output .='  <div class="avatar">';
    	$output .=  get_avatar( $id ,$size);
            $output .=' </div>';
            $output .= '<input type="file" name="simple-local-avatar" id="simple-local-avatar" />';
            $output .=' </div>';
          $output .= '<input type="submit" name="user_avatar_edit_submit" value="OK"/>';
          $output .= '</form>';
        $output .='<script type="text/javascript">var form = document.getElementById(\'your-profile\');form.encoding = \'multipart/form-data\';form.setAttribute(\'enctype\', \'multipart/form-data\');</script>';
    
         return $output;
    }
    add_shortcode('editavatar', 'editavatar_func');

    http://wordpress.org/extend/plugins/simple-local-avatars/

  2. Boba Lou Lou
    Member
    Posted 1 year ago #

    Would we put this code in the plugin itself or the function section of our theme?

  3. Ov3rfly
    Member
    Posted 1 year ago #

    @Koff: Just for info, you should be able to use the result of wp_nonce_field() directly.

    Current code:

    ob_start();
    wp_nonce_field( 'simple_local_avatar_nonce', '_simple_local_avatar_nonce', false );
    $nonce = ob_get_contents();
    ob_end_clean();

    Replacement, note the second 'false' parameter, untested:

    $nonce = wp_nonce_field( 'simple_local_avatar_nonce', '_simple_local_avatar_nonce', false, false );

    http://codex.wordpress.org/Function_Reference/wp_nonce_field

    @Boba Lou Lou: Would suggest function.php of theme - if you put it in plugin, it will be overwritten by plugin updates...

  4. voji91
    Member
    Posted 1 year ago #

    Florence said :

    Here's how I did it:

    Instal Simple Local Avatars

    Install this plugin http://wordpress.org/extend/plugins/php-code-for-posts/

    Add a new shortcode and enter this code:

    <?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>

    Copy and paste the shortcode onto a page and voilla! There's your front-end avatar uploader!

  5. jcow
    Member
    Posted 1 year ago #

    You need to make sure you include enctype="multipart/form-data" or the form will not upload an image on most servers, it will only send $_POST data, not $_FILES data.

    So the form open should be:

    <form id="your-profile" action="" method="post" enctype="multipart/form-data">

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic

Tags

No tags yet.