Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avatar files not removed from server #39

Open
thaarok opened this issue Nov 2, 2020 · 0 comments
Open

Avatar files not removed from server #39

thaarok opened this issue Nov 2, 2020 · 0 comments

Comments

@thaarok
Copy link

thaarok commented Nov 2, 2020

When I upload new avatar, the avatar file is still visible in Media page (wp-admin/upload.php), not removed from server.

Even when I call manually avatar_manager_delete_avatar($user_id) (from my testing plugin), the file is not removed from server. The function seems broken.

The implementation calls unlink() directly, which does not seems to be best practise. I recommand to use API function wp_delete_attachment() - something like:

function avatar_manager_delete_avatar( $user_id ) {
	// Retrieves user meta field based on user ID.
	$attachment_id = get_user_meta( $user_id, 'avatar_manager_custom_avatar', true );

	if ( empty( $attachment_id ) )
		return false;
	
	wp_delete_attachment($attachment_id, true); // delete attachement correctly, force remove - skip trash

	// Deletes user meta fields based on user ID.
	delete_user_meta( $user_id, 'avatar_manager_avatar_type' );
	delete_user_meta( $user_id, 'avatar_manager_custom_avatar' );

	// Determines whether Multisite support is enabled.
	if ( is_multisite() )
		delete_user_meta( $user_id, 'avatar_manager_blog_id' );

	// Calls the functions added to avatar_manager_delete_avatar action hook.
	do_action( 'avatar_manager_delete_avatar', $user_id );

	return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant