How to integrate custom database table in WordPress and using WordPress functions

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.

Read More

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.

Related posts

Leave a Reply

4 comments

  1. 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.

  2. 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.

  3. 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.

  4. As far as I know what you wanted to do can be easily achieved using Caspio. You don’t even have to use any database. They provide a online database on top of which you can create all sorts of database-driven functioinalites. Here’s a video I saw from them that shows how to build and embed a searchable contacts database on a WP site in just a few mins without any coding: https://www.youtube.com/watch?v=BgHV7ZPplo0

    Hope this helps!