Wordpress - Datenbank-Zugriffe in Plugins

Aus Wikizone
Wechseln zu: Navigation, Suche

Links

http://codex.wordpress.org/Creating_Tables_with_Plugins

Wo werden Daten gespeichert ?

  • Setup Informationen des Plugins speichert man
  • Daten die beim Benutzen des Plugins anfallen

Basics

  • Funktion schreiben die eine Tabelle erstellt
  • Diese Funktion beim Installieren des Plugins von Wordpress ausführen lassen
  • Evtl. Upgrade Funktion einbauen

Beispiel

/*** Create Table ***/ register_activation_hook( __FILE__, 'gb_install' ); //register_activation_hook( __FILE__, 'gb_install_data' );

global $gb_db_version; $gb_db_version = "1.0";

function gb_install() {

  global $wpdb;
  global $gb_db_version;
  $table_name = $wpdb->prefix . "gbheader";
     
  $sql = "CREATE TABLE $table_name (
 id mediumint(9) NOT NULL AUTO_INCREMENT,
 time datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
 text text NOT NULL,
 ip VARCHAR(55) DEFAULT  NOT NULL,
 UNIQUE KEY id (id)
   );";
  require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
  dbDelta( $sql );

  add_option( "gb_db_version", $gb_db_version );

}

// Add some data function gb_install_data() {

  global $wpdb;
  $welcome_text = "Congratulations, you just completed the installation!";
  $rows_affected = $wpdb->insert( $table_name, array( 'time' => current_time('mysql'), 'text' => $welcome_text ) );

}

/**********************/