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 on Wordpress 3.0.4 and I'm having a hard time deciding which way to go. The following is my problem:

I have a mysql database table widgets, with about 10 properties like id, size, color etc.

Now I'd like to integrate this table into Wordpress, preferably in such a way that I can get paginated lists of the widgets, show information on a single widget and have a flexible layout.

Preferably, I would like the ability to change a custom template in such a way that I can change the position of each property on the page (So maybe I'd like to put e.g. the size-property on top of the page on the left, later I might want to put it on the right bottom).

What would be the best way to store the widgets, retrieve them by using as much Wordpress build-in functions and how can I get this flexible layout as well?

I've written my own plugins before, so I have more than basic knowlegde on Wordpress/PHP/MySQL.

share|improve this question
add comment

4 Answers

up vote 8 down vote accepted

This is precisely what custom post types are for.

If it were my project, I'd scrap the custom table you have, set up a custom post type for your "widgets", add all your existing widgets as regular WP content, and use standard WordPress functions and templates to query and display them.

It's a bit of investment in the short term (if you have a lot of existing data you need to migrate), but in the long run it's best to have all your content in standard WordPress tables and display it using standard WordPress functions and templates--unless there's a really compelling reason not to.

share|improve this answer
    
Thanks MathSmath, in this case you mean that I convert the data in the table to posts? I guess in that case I would need to use post metadata in order to have the layout flexibility? –  JanWillem Jan 6 '11 at 19:34
    
Yes, you would use post metadata in order to add any number of custom fields (which correlate to your columns in your table) and then using that, create different layout based on which "widget" has which post metadata. –  Don Gilbert Jan 6 '11 at 20:11
    
Thanks a lot Don and Math, that helps me a lot! –  JanWillem Jan 6 '11 at 22:20
add comment

If this table is in same database as your WordPress installation you can use $wpdb to get data from it. Otherwise you can instantiate your own wpdb object with connection details for other database.

I am not sure what do you mean by widgets in this case, doesn't sound like same thing as WordPress widgets? Since I am fuzzy on this I have no suggestions how to handle template for those.

share|improve this answer
    
Hi Rarst, with widget I mean a 'thing', could be a book, car, wine,basically anything with properties. –  JanWillem Jan 6 '11 at 19:34
add comment

FWIW, we've done something like this where we ARE pulling the data from an external data source. We created custom post types and created functions that are triggered by different wordpress hooks, with the result that we can show everything both up front to site visitors and in wp-admin. The posts are not stored in wpdb, not even as "proxy" posts.

So it IS possible to do it that way, although we have yet to figure out a way to use the trash can to delete them, and I ended up adding in my own wp-admin pagination links using JQuery.

Unless you have a very good reason to do it this way, I agree with @MathSmath: create a custom post type and store them as "real" wordpress posts.

share|improve this answer
add comment

Please, check this plugin http://wordpress.org/extend/plugins/custom-tables/, it save me lot of work, just define your table fields and then import 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.