Automatically calling confirm_delete_users() should reasonably(safely/correctly) delete users. Automatically deleting blogs worries me, though.
wpmu_delete_blog(from wp-admin/includes/ms.php) needs a $blog_id to delete. The trick there will be to make sure you get hold of the right $blog_id to automatically delete. Users can belong to several blogs, they can make any blog they belong to as their primary blog – even the main, they can remove themselves from a blog altogether.
Also, even though $blog_id and $user_id may be the same during a setup(ie. user 63 also has blog 63), eventually $blog_id differs from $user_id sometime down the road when blogs or users outnumber each other(ie. user 1234 has blog 1543)
Yep. I would definitely not rely on the $blog_id and $user_id being the same. In most cases on my server, they’re not.
Would it be a better idea to just delete the user, and then use a separate cleanup script to delete all of the blogs that don’t have owners? I’m not entirely sure I like that, either. And, admittedly, I haven’t really looked at what happens to the blog when you delete its owner, so it may not even be feasible (if, for example, it reverts to the admin account).
In my case, the users log in, and I use wpdirauth to authenticate them against ldap. If they don’t have a blog, it creates one of the form http://mydomain/username. So when deleting, I could be able to just look up that user’s blog by the address, and get the correct id to remove. I don’t have any cases where they have multiple blogs, so it should be pretty straightforward.
Thanks for your help. I think I’m on the right track. I was mostly worried about it not working because I was calling it from outside the WP admin interface.
Just to follow up on this, I was in dependency hell when I was trying to call wpmu_delete_blog from a non-wordpress script. I ended up just setting the deleted flag in the wp_blogs table. That should prohibit access to the blog’s content, which was the main point anyway.
For users, I just looked up the id from wp_users based on username, and then deleted that userid from the wp_users and wp_usermeta tables.
So far, everything seems to be working all right.
Thanks again for your help.
I support your flagging for manual deletion. There is certainly something more comforting by having a human being giving the blog to be deleted the once over before it is too late to resurrect.
@jschinker, I am trying to do the same thing (from a plugin) but I am also in dependency hell. Is there a special way to load the WPMU api?
Are you still using WPMU? what version?
If it’s more than 3,0, then it’s multisite – not wpmu.
I’m on 3.1.2 wordpress multisite, sorry Andrea_r.
Here is an example of what I have,
<?php
/*
Plugin information, etc etc
*/
wpmu_delete_user('6');
wpmu_delete_blog('6', true);
?>
And here is what I get,
Fatal error: Call to undefined function wpmu_delete_user() in /filepath/main.php on line 12
I tried including ms.php but no matter what I include there is always another dependency.. there is probably a certain way to load the multisite api and functions?
Thanks
Also, just thought I’d add. I have this code setup on a separate php file,
// Load WordPress framework and WPMU api
include('../../../wp-load.php');
require_once('../../../wp-admin/network/admin.php');
wpmu_delete_user('6');
wpmu_delete_blog('6', true);
And when I run that file in the browser it successfully deletes users/blogs.
Sorry for the late reply here. I actually cheated. In my case, all of my blogs are http://hostname.org/username, so it was as simple as:
[Code moderated as per the Forum Rules. Please use the pastebin]
This is not done anywhere in WordPress itself. This is part of a much larger script that deletes the user system-wide.