wp_delete_user() fails during a wordpress scheduled job (3 posts)

  1. briancostea
    Posted 2 years ago #

    I have a plugin that deletes all users from a WordPress installation. The plugin registers a new menu page and when a button is clicked it deletes the users. Here is my code:

    $blogusers = get_users('blog_id=' . $GLOBALS['blog_id'] . '&orderby=nicename');
    foreach ($blogusers as $userToDelete) {

    The above deletes all of the users (except admin) no problem. However, when I put the code into a hook and schedule it, if fails on the first wp_delete_user() code.

    wp_schedule_single_event( time() + 1, 'hook_name' );

    Any reason why you can't make that call within a scheduled job but it runs fine outside of one?


  2. briancostea
    Posted 2 years ago #

    Please feel free to call me an idiot. Found this in the documentation of the function and now all is good.

    If you wish to use this function in a plugin then you must include the ./wp-admin/includes/user.php file in your plugin function, else it will throw a 'call to undefined function' error.

    Just a note that the error isn't actually thrown for some reason.

  3. lettergrade
    Posted 2 years ago #

    No name-calling here! Thanks for sharing the solution.

Topic Closed

This topic has been closed to new replies.

About this Topic