Run a script at a regular interval

I need help to create and run a script to empty specific tables across a multisite network at a regular interval (maybe daily or even hourly).

  • Kasia Swiderska
    • Support nomad

    Hello jetmac ,

    Please try this code:

    <?php
    // add daily event
    add_action( 'init', 'wpmudev_general_schedule_event_automatic_truncate_table' );
    function wpmudev_general_schedule_event_automatic_truncate_table(){
    if( ! wp_next_scheduled( 'wpmudev_general_auto_truncate_table_event' ) ){
    wp_schedule_event( strtotime('1:00:00'), 'daily', 'wpmudev_general_auto_truncate_table_event' );
    }
    }

    add_action( 'wpmudev_general_auto_truncate_table_event', 'wpmudev_general_truncate_all_table_like_mailpoet_subscriber' );
    function wpmudev_general_truncate_all_table_like_mailpoet_subscriber(){
    global $wpdb;
    if( is_multisite() ){
    $all_sites = get_sites();
    if( $all_sites ){
    foreach( $all_sites as $blog ){
    switch_to_blog( $blog->blog_id );
    // truncate
    $table_name = $wpdb->prefix .'mailpoet_subscribers';
    $table_exist = $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $wpdb->esc_like( $table_name ) ) );
    if( $table_exist ){
    $wpdb->query("TRUNCATE TABLE <code>$table_name</code>");
    }
    restore_current_blog();
    }
    }
    }else{
    $table_name = $wpdb->prefix .'mailpoet_subscribers';
    $table_exist = $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $wpdb->esc_like( $table_name ) ) );
    if( $table_exist ){
    $wpdb->query("TRUNCATE TABLE <code>$table_name</code>");
    }
    }
    }

    you can use this code a Must Use plugin.

    kind regards,

    Kasia

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.