Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm working on a plugin which creates a new table. I've got it working fine on a single site install, but I'm not so sure about multisite.

How do I create a table for each blog on network activation?

share|improve this question
add comment

1 Answer

up vote 2 down vote accepted

I would actually advise against doing that.

If you create a table for each blog on network activation you'll start running into scalability issues. Some Multisite networks are small (2-10 sites) and wouldn't cause too many issues. But larger networks (1000+ sites) would add a lot to the database if they added their plugin.

Instead, I'd recommend creating a single table and adding a "blog_ID" column to the table. If you're running a single site installation, just set that column to be 0 and run everything normally. But on a Multisite installation, set that column to whatever blog is using that data.

A single table is easier to manage.

Besides, if you create a new table for each blog, not only do you need the massive creation script when first activating the plugin, but you need to hook into the site creation mechanism to create a new table whenever a site is created.

share|improve this answer
    
I got to thinking about that after I posted the question. If it gets activated net work wide, what would $wpdb->prefix return on each individual blog? Would I be able to use that still? –  chrisguitarguy Sep 2 '11 at 16:25
    
Yes. The database prefix is set in the wp-config.php file and is used only to differentiate tables if you install two instances of WordPress to the same database (two separate instances, not a multisite setup). –  EAMann Sep 2 '11 at 16:30
    
On blog 1 (the root site), $wpdb->prefix returns the naked prefix defined i wp-config. On other sites, you get things like wp_2_ as the prefix. Any way around that? –  chrisguitarguy Sep 2 '11 at 16:40
1  
Also, you can use $wpdb->base_prefix in a single-site setup to return the same as $wpdb->prefix –  EAMann Sep 2 '11 at 16:59
1  
I just found that out, actually. Did some poking around in the wpdb class and found the set_prefix method sets base_prefix regardless. Edited the codex to include base_prefix, seemed like a thing that should be in there. –  chrisguitarguy Sep 2 '11 at 17:13
show 2 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.