WordPress.org

Make WordPress Core

#25175 closed defect (bug) (invalid)

WPMU schedule doesn't work with some functions

Reported by: Looimaster Owned by:
Milestone: Priority: normal
Severity: normal Version:
Component: Multisite Keywords:
Focuses: Cc:

Description

Here's a simple event that is scheduled to be executed daily. It executes delete_old_users() function daily.

function delete_old_users() {
    $old_users = get_site_option( 'old_users' ); // at this point it works

    foreach ( $old_users as $user_id => $expiration_time ) {
        if ( $expiration_time <= gmmktime() ) {
            wpmu_delete_user( $user_id ); // here it stops working
        }
    }
    return;
}
add_action( 'delete_old_users_daily', 'delete_old_users' );


function setup_schedule() {
    if ( ! wp_next_scheduled( 'delete_old_users_daily' ) ) {
        wp_schedule_event( time(), 'daily', 'delete_old_users_daily');
    }
}
add_action( 'wp', 'setup_schedule' );

old_users is an array(ID => timestamp_when_registered) (I know that each blog has registration date and this script will be improved to use that instead).

Error when WP_DEBUG is enabled:

[24-Aug-2013 16:28:29] PHP Fatal error: Call to undefined function wpmu_delete_user() in /home/example/ftp/example.com/wp-content/plugins/expiration/expiration.php on line 96

Theoretically, it should work, am I right? Or is it my mistake? I also tried a hook name without underscores but it didn't help.

Change History (3)

comment:1 SergeyBiryukov11 months ago

  • Component changed from General to Multisite

wpmu_delete_user() is only available in the admin: tags/3.6/wp-admin/includes/ms.php#L132.

wp-cron.php, however, always runs with front-end context.

I'd suggest adding require_once( ABSPATH . 'wp-admin/includes/ms.php' ) to delete_old_users(), it doesn't seem to have any dependencies at a glance.

comment:2 Looimaster11 months ago

You're right, it works now! I'm not sure if this qualifies for something that could/should be improved/changed or not so I'm leaving it up to you to mark this ticket as 'invalid' or keep it opened.

comment:3 SergeyBiryukov10 months ago

  • Milestone Awaiting Review deleted
  • Resolution set to invalid
  • Status changed from new to closed
Note: See TracTickets for help on using tickets.