The MU forums have moved to

Plugin Designing Tables WP and WPMU (7 posts)

  1. Adeptris
    Posted 14 years ago #

    Hi Guys,
    I am creating a couple of Plugins one for style switching and the other for templating, I have created a custom table where I added a blog_id field thinking that would do for WPMU, then I read that WPMU will create a child set of tables per blog.

    I cannot find any information on a global blog_id() in both WP and WPMU, and I want to build the plugins so they can be used in both environments.

    At the moment I have this line to identify the new table:
    $drftablename = $wpdb->prefix . "drf_plugins";

    Will $wpdb->prefix know which MU blog I am in and create the table correctly, or will it write one table in the root table list?



  2. DeannaS
    Posted 14 years ago #

    It will write one table. Typically, the $wpdb->prefix defaults to wp_. If you want a table for each blog, then you'd need to do something like:

    global $blog_id; // this gets the id of the blog you're dealing with

    $drftablename = $wpdb->prefix . $blog_id . "_drf_plugins";

    That will give you something like wp_5_drf_plugins.

    But, you CAN use the blog_id in one central table, as well - if that's all you need. Honestly, I'm not sure how things are going to work post you may want to look into that.

  3. SteveAtty
    Posted 14 years ago #

    You can simply do it using the $table_prefix (which automatically adds the blog_id when you are in WPMU).


    define('MYTABLE', $table_prefix . 'mytable');

    would if you were using wp_ as the table prefix result in

    wp_mytable in WP and wp_x_mytable in WPMU (where x is the blog_id)

    One single table for WPMU keeps the number of tables down in a large WPMU installation and if you index the blog_id column it will work quite quickly. I really wish that the WPMU developers had done that from day one rather than simply "adjusting" the table prefix to support multiple blogs. It would have been harder work in that a lot more code would have needed changing but it would have led to a much more compact database structure.

  4. Adeptris
    Posted 14 years ago #

    many thanks for clearing that up, I have gone down the one table per blog route, I am new to php and mysql but have developed in different program languages, my first plugin works in both WP and WPMU, the table I created mimics the options table with fields added for plugin name and theme name so I can tidy up when stuff is deleted, and because my plugins are theme specific and the options table lacked the extra key.

    If you do have time to have a look it is working on this MU blog for Page Styles it just loads a different css on any page, I am creating a couple of plugins as a way of learning and I thought it may be of some use to someone.


  5. SteveAtty
    Posted 14 years ago #

    I've been thinking about this.

    My wordbooker plugin needs about 3-4 tables per blog (but I'm trying to reduce that). That's fine for a WP install but a large WPMU install is the idea of 3-4 tables per blog going to put people off allowing people to use the plugin?

    I can roll up the tables into 3-4 global tables (and as I dont store a lot of data running a little migrate script as part of an upgrade process isn't going to be a major problem) and simply add a blog_id column on it

    So would that make a plugin more appealing to people running big WPMU installs?

  6. DeannaS
    Posted 14 years ago #

    Steve - yah it would. Less tables = better.

    (And thanks for catching my error on the prefix'd been a while since I'd done that.)

  7. SteveAtty
    Posted 14 years ago #

    Well thats what I was thinking. If you've got 200 blogs then that's a lot of extra tables to handle.

    I shall look into do it - I'm currently battling my way through using post_meta and user_meta to store whole chunks of data. I actually think I've worked out a way of getting it down to 2 tables, but if I have to make it 3 "global" ones then its not really too bad.

    Now all I have to do is work out the logic to give me the wp_ bit of the table name in both WPMU and WP - which I think might be:

    if (isset($db_prefix)) {
    } else{

About this Topic

  • Started 14 years ago by Adeptris
  • Latest reply from SteveAtty