我想通过cron在多站点上运行这段代码。当我使用admin\\u init钩子时,代码执行得很好,可以删除博客,但是当我将其设置为cron,然后使用WP Crontrol插件运行事件时,博客不会被删除。有什么好处?
register_activation_hook( __FILE__, \'remove_blogs_activation\' );
    /**
     * On activation, set a time, frequency and name of an action hook to be scheduled.
     */
    function remove_blogs_activation() {
        wp_schedule_event( 1386979200, \'daily\', \'remove_blogs_hook\' );
    }
    add_action( \'remove_blogs_hook\', \'remove_blogs_daily\' );
    function remove_blogs_daily() { 
        $all_blogs = wp_get_sites();    
        foreach ( $all_blogs as $key => $val ) {
            $blogs_to_keep = array( \'1\',\'2\' );
            if ( ! in_array( $val[\'blog_id\'], $blogs_to_keep ) ) {
                wpmu_delete_blog( $val[\'blog_id\'], true );          
            }
        }
    }
 我使用的新代码还用于清除用户:
    <?php
    register_activation_hook( __FILE__, \'wpchat_clear_sites_activation\' );
    /**
     * On activation, set a time, frequency and name of an action hook to be scheduled.
     */
    function wpchat_clear_sites_activation() {
        wp_schedule_event( 1386979200, \'daily\', \'daily_clear_sites_hook\' );
    }
    add_action( \'daily_clear_sites_hook\', \'wpchat_clear_out_sites_daily\' );
    /**
     * Clear out the sites and users
     */
    function wpchat_clear_out_sites_daily() { 
        require_once( ABSPATH . \'wp-admin/includes/admin.php\' );
        require_once( ABSPATH . \'wp-admin/includes/user.php\' );
        if( ! function_exists( \'wpmu_delete_blog\' ) ) return;
            $users_to_keep = array( \'1\',\'4\',\'43\' );
        $blogs_to_keep = array( \'1\',\'4\' );
        $all_sites = wp_get_sites();    
        // Remove all blogs except for the main blog and the template blog
        foreach ( $all_sites as $key => $val ) {
            $users = get_users( array( \'blog_id\' => $val[\'blog_id\'], \'fields\' => ID ) );
            // Remove all users except for the test site admins
            foreach ( $users as $user) { 
                if ( ! in_array( $user, $users_to_keep ) ) {
                    wp_delete_user( $user[\'ID\'] );
                }
            }
            if ( ! in_array( $val[\'blog_id\'], $blogs_to_keep ) ) {
                wpmu_delete_blog( $val[\'blog_id\'], true );          
            }
        }
    }
    register_deactivation_hook( __FILE__, \'wpchat_clear_sites_deactivation\' );
    /**
     * On deactivation, remove all functions from the scheduled action hook.
     */
    function wpchat_clear_sites_deactivation() {
        wp_clear_scheduled_hook( \'daily_clear_sites_hook\' );
    }
 
                    最合适的回答,由SO网友:birgire 整理而成
                    问题:当wp-cron.php 被称为,它仅包括:
require_once( dirname( __FILE__ ) . \'/wp-load.php\' );
 所以你面临的问题是
wpmu_delete_blog()
 是的
undefined 当你从你的
remove_blogs_daily() 作用
可能的解决方案:
因此,您需要添加此行:
require_once( ABSPATH . \'wp-admin/includes/admin.php\' );
 到您的代码中来修复该问题。
尝试以下示例:
function remove_blogs_daily()
{ 
    require_once( ABSPATH . \'wp-admin/includes/admin.php\' );
    if( ! function_exists( \'wpmu_delete_blog\' ) ) return;
    $all_blogs = wp_get_sites();    
    $blogs_to_keep = array( 1, 2 );
    foreach ( $all_blogs as $key => $val )
    {
        if ( ! in_array( $val[\'blog_id\'], $blogs_to_keep ) ) 
        {
            wpmu_delete_blog( $val[\'blog_id\'], TRUE );          
        }
    }
}