Create new table with Wordpress API

Posted by Fire G on Stack Overflow See other posts from Stack Overflow or by Fire G
Published on 2010-05-16T18:57:32Z Indexed on 2010/05/16 19:00 UTC
Read the original article Hit count: 202

Filed under:
|
|
|
|

I'm trying to create a new plugin to track popular posts based on views and I have everything done and ready to go, but I can't seem to create a new table using the Wordpress API (I can do it with standard PHP or with phpMyAdmin, but I want this plugin to be self-sufficient).

I've tried several ways ($wpdb->query, $wpdb->get_results, dbDelta) but none of them will create the new table.

function create_table(){
global $wpdb;
$tablename = $wpdb->prefix.'popular_by_views';
$ppbv_table = $wpdb->get_results("SHOW TABLES LIKE '".$tablename."'" , ARRAY_N);
if(is_null($ppbv_table)){
    $create_table_sql = "CREATE TABLE '".$tablename."' (
        'id' BIGINT(50) NOT NULL AUTO_INCREMENT, 
        'url' VARCHAR(255) NOT NULL, 
        'views' BIGINT(50) NOT NULL, 
        PRIMARY KEY ('id'), 
        UNIQUE ('id')
    );";
    $wpdb->show_errors();
    $wpdb->flush();
    if(is_null($wpdb->get_results("SHOW TABLES LIKE '".$tablename."'" , ARRAY_N))) echo 'crap, the SQL failed.';
}
else echo 'table already exists, nothing left to do.';}

© Stack Overflow or respective owner

Related posts about wordpress-plugin

Related posts about database