The MU forums have moved to WordPress.org

Tutorial to convert plugins ? (9 posts)

  1. dizzy99
    Member
    Posted 17 years ago #

    OK i'm not sure if this is available but i wondered if there was a tutorial anywhere on how to convert normal wordpress plugins that use database access to wordpress MU.

    I know its going to be something simple i've missed here but try as i might i cannot get them to work.

    I tried the Hover plugin as a test bed.

    https://bc-bd.org/svn/repos/tools/wordpress/plugins/trunk/hover

    It creates one table but i can't seem to get it to manually or automatically create them at plugin activation.

    I tried a variety of combinations including looking at other plugins but for some reason i can't understand why its not creating the tables.

    I even tried adding 'wp_".$i."_' to the database creation and detection plugin to no avail :(

    Anyone offer any help and advice where to look ?

  2. lunabyte
    Member
    Posted 17 years ago #

    It wouldn't necessarily do it on "activation", but what I'm doing with the menu plugin I'm writing right now is when the initial page for the plugin is loaded, it checks for the settings for the plugin with get_option. If they don't exists, it runs the function to add the table to the db, and then updates options with the default data. Then the next time the main plugin screen is called, there's data there so the create table function isn't called again.

    Simply turning on a plugin doesn't actually run any code inside that plugin.

    I guess you could tie a function to the admin menu action too, but then that function would run every time the admin menu is loaded and create some unneeded use.

    With plugins, as a note, you can declare table_prefix as a global, and then use something like:

    wpdb->query("SELECT column FROM " . table_prefix . "tablename .....");

    Which gives you the correct table prefix for the current blog/site.

  3. dizzy99
    Member
    Posted 17 years ago #

    Mmmmm this is what i thought and has me wondering why this particular plugin is misbehaving in WPMU

    It starts at the beginning by declaring itself

    'define('HOVER_TABLE', $table_prefix."hover");'

    Then it does, as you say, check to see if its database is created, if not, it is supposed to create it.

    '/* called on plugin activation */
    function hover_install () {
    global $table_prefix, $wpdb;

    /* return if our table already exists */
    if($wpdb->get_var("show tables like '".HOVER_TABLE."'")
    == HOVER_TABLE)
    return;

    /* create our table */
    $sql = "CREATE TABLE ".HOVER_TABLE." (
    id mediumint(9) NOT NULL AUTO_INCREMENT,
    search tinytext NOT NULL,
    link text NOT NULL,
    description text not NULL,
    UNIQUE KEY id (id)
    );";

    /* taken from the wordpress plugin howto */
    require_once(ABSPATH . 'wp-admin/upgrade-functions.php');
    dbDelta($sql);
    }'

    This is where i'm baffled because looking at other plugins that work perfectly fine in WPMU, i can't seem to see why this one won't create the database.

    What you've just explained is how i imagined it working and this is how the plugin presents itself. This is why i asked if there was a tutorial on conversion because i'm obviously missing something very fundemtal here.

  4. lunabyte
    Member
    Posted 17 years ago #

    This is strange. Does it call the table_prefix global when it defines itself?
    If not, it will end up with hover being the table, vice wp_X_hover, depending on where it's defined of course. If it's defined in each function, where table_prefix is called as a global, then it wouldn't matter.

    It "might" have something to do with $wpdb->get_var, but I can't say for sure.

    Or, it could be the 'dbDelta' function call. Does that exist for MU? I'm using the standard query for it in mine, which works fine, so that might be something to look into as well. It could very well be that the first check ($wpdb->get_var) returns false, but then it can't create the table using 'dbDelta'.

  5. dizzy99
    Member
    Posted 17 years ago #

    It starts with that 'define('HOVER_TABLE', $table_prefix."hover");' so i don't know if thats global or not ? Likely not.

    so i put

    'global $table_prefix;' at the top of the plugin but it didn't seem to make a difference.

    Not entirely sure about the dbDelta($sql) but i did spot it in numerous other plugins when i was looking for examples as to what could possibly be wrong.

    I really thought i understood the basics of changing a wordpress plugin (at least on database creation and paths) to a Wordpress MU plugin, but it seems in this case, the first plugin i attempt to convert has me stumped :)

    I've done some more digging at http://codex.wordpress.org/Creating_Tables_with_Plugins

    This plugin uses the activate_ call to check the database at plugin install time so i really can't understand why its not creating the databases.

    I'll keeping trying :)

  6. dizzy99
    Member
    Posted 17 years ago #

    AHA found it :)

    Turned out when a plugin is activated Wordpress checks for it in the Activate_ function but that function_ also includes a path.

    In this case the directory supplied with the plugin had a hover-0.4.1 name to it and the activate_ function was only triggering on a hover directory.

    'add_action('activate_hover/hover.php','hover_install');' so renamed the directory to hover and away it went.

    Handy to know for future reference and i've learned a little API on the way :)

  7. dizzy99
    Member
    Posted 17 years ago #

    turns out the plugin was kinda ropey anyway :) But at least i learned something :)

  8. lunabyte
    Member
    Posted 17 years ago #

    Crazy. But hey, you found it. Sounds like it's time for a beer. :D

    (Why thanks, don't mind if I do. ;) )

  9. bcbd
    Member
    Posted 16 years ago #

    I am not sure what the problem here was, if you can get back to me I'll be happy to add a check, note, whatever to catch this.

About this Topic