custom avatar removal

时间:2011-11-30 作者:boranb

您好,我已经在我的wordpress主题中实现了从前端上传的自定义头像。它正在发挥作用。用户可以添加和删除自定义头像。我的问题是,当用户选择删除他/她的自定义头像时,它会将其从头像元中删除,但上载的头像图像文件仍保留在服务器中。我想做的也是从服务器上强制删除头像的图像文件。这是user\\u profile的模板。php;

<?php
global $current_user, $wp_roles;
get_currentuserinfo();

?>
<div class="entry-content entry">
    <?php the_content(); 

      if ( isset($_REQUEST[\'error\']) && $_REQUEST[\'error\'] == \'true\' ){ 
          $error = __(\'The passwords you entered do not match.  Your password was not updated.\', \'cosmotheme\');
      }elseif( isset($_REQUEST[\'success\']) && $_REQUEST[\'success\'] == \'true\' ){
          $success = __(\'Your profile info was updated.\', \'cosmotheme\');
      }


      if ( $error ) echo \'<p class="error">\' . $error . \'</p>\'; 
      if ( isset($success) ) echo \'<p class="success">\' . $success . \'</p>\'; 

    ?>


    <?php if ( !is_user_logged_in() ) : ?>
            <p class="warning">
                <?php _e(\'You must be logged in to edit your profile.\', \'cosmotheme\'); ?>
            </p><!-- .warning -->
    <?php else : ?>
        <?php 


        ?>
<form method="post" id="adduser" action="<?php the_permalink(); ?>">
<div class="tabs-container form-profile-pic">
<?php
$action[\'group\'] = \'avatar\';
$action[\'topic\'] = \'upload\';
$action[\'index\'] = \'\';
$action[\'upload_url\'] =  home_url().\'/wp-admin/media-upload.php?post_id=-1&amp;type=image&amp;TB_iframe=true\';

$custom_avatar_url = \'\';    
$custom_avatar_meta =  get_user_meta($current_user->id, \'custom_avatar\', true);
if(is_numeric($custom_avatar_meta) && $custom_avatar_meta > 0){
$custom_avatar_url = wp_get_attachment_url($custom_avatar_meta);
}
?>
<label for="profile-pic"><?php _e(\'Profile picture\', \'cosmotheme\'); ?></label>
<p class="button"><input type="button" class="button-primary front_post_input" id="upload_btn" value="<?php _e(\'Choose file\',\'cosmotheme\'); ?>" <?php echo fields::action( $action , \'extern-upload-id\' ) ?>  /></p>
<input type="text" name="image" value="<?php echo $custom_avatar_url; ?>" class="generic-record front_post_input" id="avatar_upload"  onclick="jQuery(\'#upload_btn\').click();"  />
<label class="remove-avatar" for="remove_avatar"><input type="checkbox" name="remove_avatar" <?php echo $disabled ?> value="1" id="remove_avatar"> <?php _e(\'Remove avatar\', \'cosmotheme\'); ?></label>
<input type="hidden" name="avatar_id" id="avatar_upload_id" value="<?php echo $custom_avatar_meta; ?>"  class="generic-record generic-single-record " />
</div><!-- .profile pic -->  
            <div class="tabs-container form-username">
                <label for="user-name"><?php _e(\'Username\', \'cosmotheme\'); ?></label>
                <input class="text-input" name="user-name" type="text" id="user-name" disabled value="<?php the_author_meta( \'user_login\', $current_user->id ); ?>" />
            </div><!-- .form-username -->
            <div class="tabs-container form-username">
                <label for="first-name"><?php _e(\'First name\', \'cosmotheme\'); ?></label>
                <input class="text-input" name="first-name" type="text" id="first-name" <?php echo $disabled ?> value="<?php the_author_meta( \'user_firstname\', $current_user->id ); ?>" />
            </div><!-- .form-username -->
            <div class="tabs-container form-username">
                <label for="last-name"><?php _e(\'Last name\', \'cosmotheme\'); ?></label>
                <input class="text-input" name="last-name" type="text" id="last-name" <?php echo $disabled ?> value="<?php the_author_meta( \'user_lastname\', $current_user->id ); ?>" />
            </div><!-- .form-username -->
            <div class="tabs-container form-email">
                <label for="email"><?php _e(\'E-mail *\', \'cosmotheme\'); ?></label>
                <input class="text-input" name="email" type="text" id="email" <?php echo $disabled ?> value="<?php the_author_meta( \'user_email\', $current_user->id ); ?>" />
            </div><!-- .form-email -->
            <div class="tabs-container form-url">
                <label for="url"><?php _e(\'Website\', \'cosmotheme\'); ?></label>
                <input class="text-input" name="url" type="text" id="url" <?php echo $disabled ?> value="<?php the_author_meta( \'user_url\', $current_user->id ); ?>" />
            </div><!-- .form-url -->
            <div class="tabs-container form-password">
                <label for="pass1"><?php _e(\'Password *\', \'cosmotheme\'); ?> </label>
                <input class="text-input" name="pass1" <?php echo $disabled ?> type="password" id="pass1" />
            </div><!-- .form-password -->
            <div class="tabs-container form-password">
                <label for="pass2"><?php _e(\'Repeat password *\', \'cosmotheme\'); ?></label>
                <input class="text-input" name="pass2" <?php echo $disabled ?> type="password" id="pass2" />
            </div><!-- .form-password -->
            <div class="tabs-container form-textarea">
                <label for="description"><?php _e(\'Biographical information\', \'cosmotheme\') ?></label>
                <textarea name="description" id="description" <?php echo $disabled ?> rows="3" cols="50"><?php the_author_meta( \'description\', $current_user->id ); ?></textarea>
            </div><!-- .form-textarea -->
            <p class="form-submit">
                <?php //echo $referer; ?>
                <input name="updateuser" type="submit" id="updateuser" <?php echo $disabled ?> class="submit button" value="<?php _e(\'Update\', \'cosmotheme\'); ?>" />
                <?php wp_nonce_field( \'update-user\' ) ?>
                <input name="action" type="hidden" id="action" <?php echo $disabled ?> value="update-user" />
            </p><!-- .form-submit -->
        </form><!-- #adduser -->
        <?php endif; ?>
    </div><!-- .entry-content -->  
这是user\\u profile\\u更新。php;

<?php

/* Get user info. */
global $current_user, $wp_roles;
get_currentuserinfo();

/* Load the registration file. */
//require_once( ABSPATH . WPINC . \'/registration.php\' );

/* If profile was saved, update profile. */
if ( \'POST\' == $_SERVER[\'REQUEST_METHOD\'] && !empty( $_POST[\'action\'] ) && $_POST[\'action\'] == \'update-user\' && ( !defined(\'IS_FOR_DEMO\' ) || get_the_author_meta( \'user_login\', $current_user->id ) != \'demo\') ) {

    /* Update user password. */
    if ( !empty($_POST[\'pass1\'] ) && !empty( $_POST[\'pass2\'] ) ) { 
        if ( $_POST[\'pass1\'] == $_POST[\'pass2\'] ){
            wp_update_user( array( \'ID\' => $current_user->id, \'user_pass\' => esc_attr( $_POST[\'pass1\'] ) ) );
        }else{
            $error = __(\'The passwords you entered do not match.  Your password was not updated.\', \'cosmotheme\');
        }
    }

    /* Update user information. */
    if ( !empty( $_POST[\'url\'] ) )
        update_user_meta( $current_user->id, \'user_url\', esc_url( $_POST[\'url\'] ) );
    if ( !empty( $_POST[\'email\'] ) )
        update_user_meta( $current_user->id, \'user_email\', esc_attr( $_POST[\'email\'] ) );
    if ( !empty( $_POST[\'first-name\'] ) ) {
         update_user_meta( $current_user->id, \'first_name\', esc_attr( $_POST[\'first-name\'] ) ); 
    }
    if ( !empty( $_POST[\'last-name\'] ) )
        update_user_meta($current_user->id, \'last_name\', esc_attr( $_POST[\'last-name\'] ) );
    if ( !empty( $_POST[\'description\'] ) )
        update_user_meta( $current_user->id, \'description\', esc_attr( $_POST[\'description\'] ) );

    if ( !empty( $_POST[\'avatar_id\'] )  && $_POST[\'avatar_id\'] != get_user_meta($current_user->id, \'custom_avatar\', true) && empty( $_POST[\'remove_avatar\'])){ 
        update_user_meta( $current_user->id, \'custom_avatar\', esc_attr( $_POST[\'avatar_id\'] ) );
    }elseif( !empty( $_POST[\'remove_avatar\']) && $_POST[\'remove_avatar\'] == 1){
        update_user_meta( $current_user->id, \'custom_avatar\', \'-1\' );
    }
    /* Redirect so the page will show updated info. */
   // if ( !$error ) {
        $redirect_url   = get_permalink();
                if(isset($error) && $error != \'\'){  
                    $msg        = array( \'error\' => "true" );
                }else{  
                    $msg        = array( \'success\' => "true" ); 
                }
                $_url    = add_query_arg( $msg , $redirect_url );

        wp_redirect( $_url );
        exit;
   // }
}
?>
正如你所看到的,如果用户检查输入“删除头像”,我将更新custom\\u avatar meta-1,这将使其无法显示。这是可行的,但正如我之前所说,我还需要强制从服务器上删除图像文件。你能帮帮我吗?谢谢

1 个回复
最合适的回答,由SO网友:Chris_O 整理而成

您只需要编写一个delete avatar函数,该函数获取文件的位置,并使用php将其删除unlink function.

下面的代码可以作为如何完成的示例。当用户上传新的头像时,我使用它来删除旧的用户头像。它还用于替换默认的化身,这些化身曾经存储在不再使用的旧插件的自定义目录中。

function avatar_delete( $user_id ) {
            $old_avatars = get_user_meta( $user_id, \'author_avatar\', true );
            $auth = get_the_author_meta( \'user_login\', $user_id );
            $wp_content = WP_CONTENT_DIR;
            $filename = $wp_content . \'/images/authors/\' . $auth . \'.jpg\';
            if ( file_exists( $filename ) ) {

                unlink( $filename );
            }
            else {
                $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, \'author_avatar\' );
        }

结束

相关推荐

How to change user`s avatar?

有没有办法在没有插件的情况下改变用户的头像?为什么在用户>你的个人资料中没有“头像上传”部分?我不能使用插件。我是失明还是被迫使用Gravatar/