//Every 15 minutes
$schedules['every15minutes'] = array(
'interval' => 900,
'display' => __('Every 15 minutes')
);
//Every 30 minutes
$schedules['every30minutes'] = array(
'interval' => 1800,
'display' => __('Every 30 minutes')
);
return $schedules;
///////////////////////////////////////////////////////////////////////////
//MAKE CHANGES HERE
$filename = "my-db-table.csv"; //Just the filename you want to use WITHOUT the path. Include .csv extension
$columns = "column2,column3,column4"; //Comma seperated list of columns to retrieve from DB in MySQL query
$table_name = "mytablename"; //Just the DB table name by itself WITHOUT the prefix
$where = "WHERE somecolumn='sometext'"; //MySQL WHERE options or set to "" for none. Example: WHERE somecolumn='sometext'
$order_by = "ORDER BY anothercolumn DESC"; //MySQL ORDER BY options or set to "" for none. Example: ORDER BY anothercolumn DESC
$limit = "LIMIT 250"; //MySQL LIMIT options or set to "" for none. Example: LIMIT 250
$header_row = $columns; //Comma seperated list to use as header row in CSV file. Set to "" for no header row. Default is set to = $columns;
//DO NOT CHANGE ANYTHING BELOW THIS LINE UNLESS YOU KNOW WHAT YOU ARE DOING
///////////////////////////////////////////////////////////////////////////
global $wpdb;
$table_name = $wpdb->prefix.$table_name; //Add prefix to table name
$query = "SELECT {$columns} FROM `{$table_name}` {$where} {$order_by} {$limit}"; //Create MySQL query string
$results = $wpdb->get_results($query, ARRAY_A); //Do MySQL databse query
if ($header_row != "") {
$output = $header_row; //Set header row for CSV file
} else {
$output = ""; //No header row in CSV file
}
foreach ($results as $row) {
$output = $output."\r\n";
$columns_array = split(",",$columns);
$first_column = true;
foreach ($columns_array as $column) {
if ($first_column != true) {
$output = $output.",";
} else {
$first_column = false;
}
$output = $output.$row[$column];
}
}
//WARNING: Files written to plugin_dir_path are PUBLICLY accessible in the /wp-content/plugins/auto-export-db-table-to-file/ directory.
$filename = plugin_dir_path( __FILE__ ).$filename; //Add path to plugin directory
$file_handle = fopen($filename, 'w'); //Open file for write
fwrite($file_handle, $output); //Write output to file
fclose($file_handle); //Close file