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.

Getting started on Wordpress just today. I noticed that Wordpress created tables in a mySql database (with name *.wrdp1) when I first set it up. These tables cover comments, posts, users, and some link-relationships.

Now, let's say I would like to work with a new set of data. Suppose I were to create a page with two things:

  • a textfield, where my users can enter a number between 1 and 10
  • a chart, which displays the frequency of numbers historically entered by my users

My guess: For something like the above, I suppose I will have to set up a custom table/data-set in the database, such that this number data can be written and then read. (I am also guessing that I will have to use some PHP, in order to store the number from the textfield to the database, and later to read the numbers back.)

Can someone outline what a recommended approach in this case would be, roughly? (I can thereafter research the exact details myself.)

share|improve this question
add comment

2 Answers

up vote 1 down vote accepted

WordPress has a pretty flexible way of handling custom data, and it stores it in a table called wp_postmeta (assuming default wp_ prefix). The only catch is that it assumes that it will somehow be related to a certain post, which I'm not sure you actually want to do.

It's very simple to use (see here for more info):

// Add a custom data (i.e. textfield of value '5') for post ID = 1
update_post_meta(1, 'my_textfield', 5);

// Return an array which contains 'my_textfield' values from post ID = 1
get_post_meta(1, 'my_textfield', true);

If the information is not related to a post, but rather to a user, you can also take advantage of the wp_usermeta table. The idea is the same (see here for more):

// Add a custom data (i.e. textfield of value '5') for to user 1
update_user_meta(1, 'my_textfield', 5);

// Return an array which contains 'my_textfield' values for user 1
get_user_meta(1, 'my_textfield', true);

Creating a custom table should be a last resort, and only used when the custom information you want to store has more than one value (i.e. you need to know the number chosen by the user, the time it was recorded, the IP of that user, etc). In that case, you'll need to code your own functions to add values to that table, not to mention the PHP code which will create that table in the first place. It's more complicated, anyone who's done it before will attest that it's a delicate affair and hard to debug, plus there's no guarantee that it won't break in future WordPress versions. Anyhow, if that's what you need, this should get you going.

Hope it helps! Let us know how it goes.

share|improve this answer
    
+1 for describing methods for both the "native" way and the customized PHP way; this should get me started. –  boardbite Sep 24 '12 at 7:51
    
Glad it helped! –  tbuteler Sep 24 '12 at 12:54
add comment

To add to @tbuteler's answer, you also have Custom Post Types which can have their own associated meta data. If you look at the Post Types page, you'll see how WordPress uses these in some non-obvious ways, like storing nav menu items. There are also Custom Taxonomies which can be used to create relationships like the built in categories and tags. As with meta data, there's a whole set of API functions to manage these objects to allow you to build your own UI for managing your data.

share|improve this answer
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.