开发者

What is best practice for working with DB in Wordpress?

I'm developing a plugin for Wordpress, and I need to store some information from a form into a table in the DB.

There are probably lots of pages explaining how to do this, this being one of them: http://codex.wordpress.org/Function_Reference/wpdb_Class

But are there any other pages talking about best practice for interacting with th WP DB?

UPDATE Found a few more pages which could be usefull:

http://wpengineer.com/开发者_C百科wordpress-database-functions

http://blue-anvil.com/archives/wordpress-development-techniques-1-running-custom-queries-using-the-wpdb-class


Unless you need to create your own complex table structure I'd suggest using the existing tables for your needs. There's the options table, user meta table, and post meta table to work with. These all have built in apis for quick access.

  • Options: add_option(), get_option(), update_option(), delete_option()
  • Usermeta: add_user_meta(), get_user_meta(), update_user_meta(), delete_user_meta()
  • Postmeta: add_post_meta(), get_post_meta(), update_post_meta(), delete_post_meta()

I've not found much real need to go outside of these tables (yes, there are exceptions, I've done them myself when the data needs are complex) but these meta options all use a text field in the db to store the data, so there's a lot you can store here if its simple data.

If your data is simple then consider storing your information in one of these places as individual options or even as serialized arrays.


One of my BIGGEST pet peeves with plug in developers that leverage the WP database is that if/when a given database driven plugin isn't used anymore, the developer doesn't think to remove the footprint it made in the database.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜