• I’m continuing my work on a WordPress site that uses forum posts as content (more on this can be found here and here).

    After creating news section which takes first post of every topic and turns it into a WordPress post I need something more complicated – a section of articles. This requires taking the first post of a topic and appending the remaining posts to the first one – just like chapters in the article. And actually this is something that I’ve already achieved. I’m using a second query to get posts from a topic, and by using variables for topic IDs and post IDs I can even specify which posts will be imported and appended. The problem is I need to be able to add, edit or remove topics and posts IDs from the query. After using settings API in the news section I thought that it wouldn’t be too difficult to use it here, but sadly it is. And this is where I need help.

    What I need is an options page that will list all the topics and posts IDs, and let me add, edit or delete these settings and will update the list. I tried to use the settings API, to add a pair of forms every time an option is added, but I’m not even close to what I need. I’m not even sure if storing a multidimentional array ( article# => array( topic_id => ‘value’, posts_ids => ‘values’ ) ) as an option is a good idea. I’ll be grateful for any ideas.

Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator bcworkz

    (@bcworkz)

    Hi Piotr! I see you are keeping busy 🙂

    I agree the Settings API is not really setup for what you have in mind. I think you could leverage the WP_Posts_List_Table class (which displays all WP posts) to meet you needs. You were anxious to learn something else new right?

    Unfortunately, I don’t think there is much documentation available. All I’ve seen is oriented towards customizing existing tables, not creating your own table. Fortunately, you could probably use the existing class with only a few changes. You can either copy it and rename it or extend it and only define those methods you need to change.

    If that all seems too much, you can at least be inspired by how it is laid out and used for coming up with your own table. I wouldn’t worry too much about storing complex arrays in the options table. In theory, such structures can be serialized and unserialized as easily as simpler structures. There have been issues where data gets double serialized upon saving but only single unserialized upon retrieval, but that should be easily resolved if it arises. The value column is longtext, so you shouldn’t run into size issues.

    Designing a good user interface (and the underlying data structure that supports it) is always a critical step in software development. Changing it once significant data has been created can prove to be difficult, best to get it right the first time. Or build in enough flexibility so that changes are not painful. Try not to worry too much about how hard or easy it is to code, focus on what works for the user. If something turns out to be quite difficult, it can be re-visited.

    Thread Starter Piotr

    (@p396)

    Well, this is surprising, I did something completely different from what you suggested and it works. 😉 This was my inspiration. I made a form similar to the one in the tutorial. It creates an array which I’m saving in the options table using AJAX. JQuery lets me add new fields and a simple PHP loop inserts values of previously saved options, so I can edit or even delete them from the form. It’s exactly what I wanted. It appears that I have some problems with my JQuery – appended fields aren’t being validated for some reason – I’m sure it’s some newbie mistake that I made. I’ll find it sooner or later.

    But I need one more advice on my articles section. As I stated before, I’m appending forum posts as article chapters. I could append them with ‘<!––nextpage––>’ so every chapter would be a separate page. This would be nice, but – no surprise here, I hope – I want something more. An index of those chapters, with titles., preferably on the sidebar. I have some ideas, but I need to know if I’m on the right track.

    First thing, the pagination – on many sites it takes form of links ‘previous’ and ‘next’, but there are ways to make it look like ‘Page: [1] [2] [3] [4]’. If there are page numbers, there could be page titles, I just need a way to put them there. I’m thinking of two solutions. 1. putting the titles in my options table, maybe even using the form that I created or 2. putting the titles in a tag on my forum which after importing WordPress would see as a shortcode. In both cases I’m not sure how to use them later as links in the chapter list.

    And the second thing is actually putting the list on the sidebar. Would it require creating a widget? And how to connect it to the post that is currently displayed?

    I realize that those two problems may be easy to solve but until now I’ve been focusing on the backend functionality. I just need to know where to start and what to look for. So once again – I’m asking for a little help. 😉

    Moderator bcworkz

    (@bcworkz)

    I’m pleased you found a good solution. We have a saying here, “There’s more than one way to skin a cat.” A little crude taken literally, sorry. It’s just a saying, I can’t imagine why anyone would really want to skin a cat 🙂

    I’m confused about why you would want to store titles somewhere. If these articles are composed of posts, why could you not use the post title that is already in the database with the post? You mentioned maybe using ‘<!––nextpage––>’. So you are actually concatenating various post’s content together and creating new content from the result? Is that necessary? Could you not run a loop to generate an article dynamically?

    It is fine if you need to do it that way, I’m only wondering if you are overlooking a better way. If the association with the original posts is lost, I think I would store the titles in the content as shortcode content in preference over storing them in the DB somewhere.

    If the article remains a separate post content, you make a table of contents by running the loop twice. Once just for the titles in a tble of contents, then again for actual contents. If the article exists as separate text, code would need to parse through the text to find the title shortcodes and compose table of contents from that.

    You can put contents in the sidebar by putting the code directly on the sidebar template, no widget required. It must then be either before or after all other widgets. To be able to move it around like other widgets, it then needs to be a widget.

    Thread Starter Piotr

    (@p396)

    Oh my, poor little cat. 😉

    About those articles – the problem is that a single article on my site is composed of posts from one forum topic. Only the topic has a title in the database, there is no title for a single post. This is why I need to store the titles somewhere. There may be better ways, I hope I’ll find them, but for now I think this isn’t a bad solution. And it works.

    Thank you for the tips. I hope that’s all I need to complete this.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Dynamically adding forms and options’ is closed to new replies.