سؤال

Hello i have implemented a custom avatar upload from front-end in my wordpress theme. It is working. User can add and remove customa avatar. My problem is when user selects to remove his/her custom avatar, it is removing it from avatar meta but the uploaded avatar image files remains in the server. What i want to make is also force delete the image files of avatar from the server. This is the template of user_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 -->  

And this is the user_profile_update.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;
   // }
}
?>

As you can see if the user checks the input "remove avatar" i am updating the custom_avatar meta -1 which makes it invisible to show. This is working but as i said before, i also need to force delete the image file from the server. Can you help me? Thanks.

لا يوجد حل صحيح

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى wordpress.stackexchange
scroll top