[wp-hackers] Plugin Questions

Scott Merrill skippy at skippy.net
Thu May 12 14:50:59 GMT 2005

Bill Smith wrote:
> 1) I'm working on a small plugin that requires an additional database
> table. Currently I'm using a query like
> SHOW TABLES LIKE 'wp_mytable'
> to check to see if my table already exists and create it if it doesn't.
> That seems expensive to me to do everytime my plugin is loaded so I was
> thinking about socking somthing away in wp_options that would let me
> know that I've already created it. I'm assuming that by the time an
> plugin code is executed that the options will have already been queried
> and cached.
> What are others doing to accomplish this?

Here's what I do in my subscribe2 plugin, which uses a custom table:
function s2_install() {
// include upgrade-functions for maybe_create_table;
if (! function_exists('maybe_create_table')) {
        require_once(ABSPATH . '/wp-admin/upgrade-functions.php');

global $table_prefix;
$s2_table = $table_prefix . "subscribe2";
$s2_table_sql = "CREATE TABLE " . $s2_table .
     "( id int(11) NOT NULL auto_increment,
     email varchar(64) NOT NULL default '',
     active tinyint(1) default 0,
     PRIMARY KEY (id) )";

// create the table, as needed
maybe_create_table($s2_table, $s2_table_sql);

} // s2_install

function s2_options() {
global $wpdb, $table_prefix, $cache_categories;

$s2_table = $table_prefix . "subscribe2";

// check if we need to install the table
$sql = "SELECT COUNT(id) FROM " . $s2_table;
// turn errors off, for the momemnt
$errors = $wpdb->hide_errors();
$foo = $wpdb->get_var($sql);
// turn errors back on
$errors = $wpdb->show_errors();
if ('' == $foo) { s2_install(); }
.... more stuff here....

> 3) Last one... for the moment. :) In certain parts of my plugin, I'd
> like to restrict access to either the admin or users of a certain level.
> Is there a call that can tell me the users credentials so I can
> determine what they are allowed to do?

get_currentuserinfo() usually works; though there might be some
complication now with the "pluggable functions" recently introduced.

Be sure to read these:

skippy at skippy.net | http://skippy.net/

gpg --keyserver pgp.mit.edu --recv-keys 9CFA4B35
506C F8BB 17AE 8A05 0B49  3544 476A 7DEC 9CFA 4B35

More information about the wp-hackers mailing list