· 6 years ago · Jun 04, 2019, 09:00 AM
1function ficharme_create_plugin_database_table()
2{
3 global $table_prefix, $wpdb;
4
5 $tblname = 'buzz_registro_fichaje';
6 $wp_track_table = $table_prefix . "$tblname";
7
8 #Check to see if the table exists already, if not, then create it
9
10 if($wpdb->get_var( "show tables like '$wp_track_table'" ) != $wp_track_table)
11 {
12
13
14 $sql = "CREATE TABLE `". $wp_track_table . "` ( ";
15
16 $sql .= " `registro_id` int(11) NOT NULL auto_increment, ";
17 $sql .= " `user_id` int(11) NOT NULL, ";
18 $sql .= " `hora_registro` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, ";
19 $sql .= " `tipo_registro` TEXT NOT NULL, ";
20 $sql .= " `ip` TEXT NOT NULL, ";
21
22
23 $sql .= " PRIMARY KEY (`registro_id`) ";
24 $sql .= ") ENGINE=MyISAM DEFAULT CHARACTER SET=utf8mb4 COLLATE=utf8mb4_unicode_520_ci AUTO_INCREMENT=1 ; ";
25
26 require_once( ABSPATH . '/wp-admin/includes/upgrade.php' );
27 dbDelta($sql);
28 }
29}
30
31register_activation_hook( __FILE__, 'ficharme_create_plugin_database_table' );
32
33/**
34*
35* Código para multisite
36*
37*/
38
39function ficharme_on_create_blog( $params ) {
40
41
42 //Comprobamos si el plugin está activado para multisite, en este caso el plugin es gestion-ficharme
43 if ( is_plugin_active_for_network( 'gestion-ficharme/gestion-ficharme.php' ) ) {
44
45 switch_to_blog( $params->blog_id );
46 ficharme_create_plugin_database_table(); //Llamamos a la función que crea la tabla
47 restore_current_blog();
48 }
49}
50add_action( 'wp_insert_site', 'ficharme_on_create_blog' );