I create a new theme.

One of my targets is to enable the admin to add unique text (html code) to unique table which the theme will automatically create (I mean something like wp_myhtml).

My questions are:

  1. How to define that the theme will create a new table only at the first time the admin activate it (if the table exists - don't create again. I know to do it with pure PHP / SQL, but maybe there is an option with WordPress to create custom tables)?

  2. How to CURD rows from this table via the panel? and only read via the website itself? are there unique functions to do this?

I don't use options functions (add_option, update_option, etc) because the html codes can be very long, and one field of LONGTEXT won't handle it.

In addition, I don't use the editor (Appearence > Editor) because I need it in the DB.

  • 1
    use a custom post type to store the data, this does not need its own table. – Milo Jan 30 '13 at 17:58
  • can't. it is definitely not a post type and not even closer, trust me. :-) – Luis Jan 30 '13 at 18:18
  • 1
    it sounds exactly like something that could be stored as a post type. you don't have to enable the UI or make it public, you are just using the WP posts table to store the data, and the API to save/retrieve the data. there are very few reasons to ever have to create a new table, and far more reasons not to. – Milo Jan 30 '13 at 18:23
  • 1
    This question, as written, is too vague to answer, and looks to be too broadly scoped. Can you more specifically describe the data you want to add to this custom table? Also, you should probably ask Question #1 first, and then once you have an answer, ask Question #2 as a separate question. – Chip Bennett Jan 30 '13 at 18:25
  • it's something like template system. each template has html and css. when adding a template (or edit), the admin sees in the page a title (template's title), a parent-template and a table with 3 columns - html (editable), css (editable), preview. that why I don't think post type is for this target. In addition, the templates should be displayed in different way and not the way of sorting titles. – Luis Jan 30 '13 at 19:09

I'm adding this as an answer, even though it doesn't strictly answer the question you've asked, but it is a means to achieve what you're after.

As I mentioned in my comment, you can use a custom post type and post meta data to store this data, you don't necessarily have to use the WordPress provided UI to manage the data.

When you register a post type, you can set the public argument to false, this way your post type will not be displayed in the admin menu or the front end, it is entirely up to you to provide the UI to manage this data. An example of this is the built in nav menu interface, which behind the scenes is actually another post type.

Using a post type will go a long way toward providing what you need to make this work, without having to write a lot of code to create and update your data. You can use wp_insert_post, wp_update_post and wp_delete_post to insert / update / delete your templates. The post table has a parent field you can use to assign parent templates. Posts can have associated meta data via custom fields you can use to store additional fields of data, and you can use taxonomies to further categorize or organize data.

Also note, regarding your comment:

In addition, the templates should be displayed in different way and not the way of sorting titles.

If you do choose to use the built in post type UI, you can modify the columns and sorting as you like, search this site for custom admin columns, there are several questions with examples on how to do this. You can also add meta boxes to the post edit view to provide UI for additional fields, like your html and preview.

  • Ok, I think you convinced me. I didn't know that postmeta is a table, I thought it's a small field in posts table so I can't store there a lot of code, but postmeta is a table and meta_value is LONGTEXT field as I need, so I can store the CSS text as a meta_key and inside posts > post_content I will store the HTML. THANK YOU! – Luis Jan 30 '13 at 22:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.