WordPress.org

Ready to get started?Download WordPress

Forums

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

  1. briancostea
    Member
    Posted 11 months 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) {
       wp_delete_user((integer)$userToDelete->ID);
       }
    }

    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?

    Thanks!

  2. briancostea
    Member
    Posted 11 months 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
    Member
    Posted 11 months ago #

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

Reply

You must log in to post.

About this Topic