Welcome to the Question2Answer Q&A. There's also a demo if you just want to try it out.
0 votes
1.2k views
in Plugins by
I think it would be extremely helpful to allow for additional fields on the Ask a Question form and for these fields to be able to be displayed whenever you post and view a question. One simple application, is to easily allow for images to be associated with each question.

Wordpress has this as a built in automatically with its "Custom Fields" feature and interestingly Q2A seems to be already set up to do this via its "qa_postmetas" table.

Hopefully, the ability to add unlimited fields to any question will be part of the core in upcoming versions (like in Wordpress), but in the interim, can someone provide some sort of guidance on how one would develop a plugin for this kind of application? Do we only need a Layer?

This would seem to be a very simple plugin to develop since the "qa_postmetas" is alrady set up to support multiple fields per question, but I really have no idea where to start on this and would appreciate any feedback.

Thanks.
Q2A version: 1.5 release

2 Answers

+1 vote
by

Sure, you could add the fields to the ask form, and display them on the module page, via a layer. You could also perform validation on them via a filter plugin which retrieves the field values directly from qa_post_text() since they won't be passed to the plugin directly. Finally you could keep them in the qa_postmetas table with keys which start with a prefix which identifies your plugin, so they won't clash with other keys in future.

by
Thanks. Can you give me some guidance on how I would add the fields to the ask form? I assume I just have to add another field to the form array? How would I intercept the form array to add these fields in the right place? Are there any code examples you can give?

Also, after someone enters the data for the new custom fields, I would use the qa_db_postmeta_set to save the fields to the database, correct? But, how would I process this input, in the same layer?
by
One more question, if I wanted to add these fields on the edit page for a question, which of the core pages handles the editing of questions, qa-page-question.php?
by
The edit page for a question is generated by qa-page-question.php (using some functions in other files it includes) but that's not so relevant - you should modify the edit form using your layer, as for the ask question form.
+1 vote
by

if you're using a plugin layer, checking for things like

if($this->template == 'ask')

, for example, works.  This is how the polls plugin does it:

                else if($this->template == 'ask' && !qa_user_permit_error('permit_post_q') && !qa_opt('site_maintenance') && qa_permit_check('permit_post_poll')) {
                    $this->content['form']['tags'] .= ' onSubmit="pollSubmit(event)"';
                    $this->content['form']['fields'][] = array(
                        'label' => qa_opt('poll_checkbox_text'),
                        'tags' => 'NAME="is_poll" ID="is_poll" onclick="jQuery(\'#qa-poll-ask-div\').toggle()"',
                        'type' => 'checkbox',
                    );
                    $this->content['form']['fields'][] = array(
                        'note' => '<div id="qa-poll-ask-div" style="display:none"><p class="qa-form-tall-label"><input type="checkbox" name="poll_multiple">'.qa_opt('poll_multiple_text').'</p><p class="qa-form-tall-label">'.qa_opt('poll_answers_text').'</p><input type="input" class="qa-poll-answer-text" class="qa-poll-answer-text" name="poll_answer_1" id="poll_answer_1">&nbsp;<input type="button" class="qa-poll-answer-add" value="+" onclick="addPollAnswer(poll_answer_index)"></div>',
                        'type' => 'static',
                    );
                }

 

I haven't used the built in meta functions yet, but they look simple enough.  qa_db_postmeta_get is the other one you need.

 

by
Hi Noah,
Thanks, the poll plugin was very helpful and I got this working. The built in meta functions are very easy. I ran into another problem, though, with saving information to the database. I posted the issue here:
http://www.question2answer.org/qa/12595/plugin-saving-information-database-question-without-changing

Take a look if you can, as maybe you have run into this with other plugins.
...