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 understand its advantage for something which will be released in public, but does it do any good for a developer by saving efforts as the code evolves over time? Like right now, I am hardcoding the table name in my queries, what if I was to change the name or something?

share|improve this question
    
You could use class or global constants... sry, got nothing about dbdelta. –  kaiser May 2 '11 at 13:14
add comment

1 Answer

up vote 1 down vote accepted

The purpose of dbDelta() is both to create and update tables. If you don't need updates then you can skip it.

For keeping track of name you can simply follow WP convention and save name of your table in field of $wpdb object.

Also note that you should consider there is possibility of code running on multisite. Even if you are not releasing it you might need to run it that way one day and then you are in for a painful refactoring if all table-related stuff is hardcoded.

share|improve this answer
    
Can you please elaborate a bit on how to add custom table names to $wpdb object? Good point for multisite, and I think using the wpdb object for storing names will remove the multisite issue too. Right? –  Ashfame May 2 '11 at 15:24
    
@Ashfame global $wpdb; $wpdb->custom = 'custom'; It's easy to forget that $wpdb is just an object and as any object in PHP you can go and create arbitrary fields in it. Yep on multisite, you will need to make use of WP database prefixes appropriately. –  Rarst May 2 '11 at 16:16
    
Yeah! I thought there is some other way to make sure it doesn't miss anything or misbehave somehow like in some recommended way. Should I just do it anywhere in the functions.php or it would be better to do it on a hook like init? I think as soon as it is before the usage, it doesn't make a difference, right? Regarding multisite $wpdb->custom will itself adapt to the blog ID or I have to do that somehow? and how? –  Ashfame May 2 '11 at 16:21
    
@Ashfame on init should work just fine and possible issues are name collision (don't pick common words) or if something completely replaces $wpdb object (unlikely). For multisite you will need to prefix table name, prefix is numbered on multisite and will be handled to be correct for current site. –  Rarst May 3 '11 at 9:07
    
Great! Thanks :) –  Ashfame May 3 '11 at 12:42
add comment

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.