[Tutorial] Insert/Import Questions/Answers to Anspress Programmatically

3.45K viewsGeneralfeatures
0

Hi,

As Rahul has already posted a FAQ about the same –

https://anspress.io/resources/faq/insertimport-questions-programmatically/

But it has some open issues, which I have already asked in another question. Here, I am writing my work around using the same FAQ to import/insert questions/answers programmatically.

 

Inserting/importing question is quite easy, as the core function is already been adapted for it. So it’s pretty much same as mentioned in FAQ. Only additional thing I did was to insert/update qameta and activity on asking new question.

However inserting/importing answer gave me a headache, as its core function require some modifications, to use it out of the box. Some of the code blocks from function submit_answer_form, which is causing issue are –

`$question_id = ap_sanitize_unslash( ‘question_id’, ‘r’ );`

`$form->save_values_session( $question_id );`

`if ( $form->have_errors() ) {…}`

 

So I created a new custom function in my child theme, which is nothing but the same as submit_answer_form but tweaked it as per my use (in short, I provided parent_id for the answer in the function args).

I can’t seem to be able to embed gist code here (maybe anyone with access can edit this post and embed it), following is the link of function which one should add in his child theme’s functions.php

https://gist.github.com/atultiwari/53471873ac622b2cf039efebf897c2b5

 

Following is the link for function, which can be used/modified for inserting/importing question/answer programmatically (please note that, for my usage I needed multiple tags, so I created an array for it named `$anspress_tags_id` in the function)-

https://gist.github.com/atultiwari/29ddff5fa59c97153061d3068db1531f

 

P.S. – I used latest master build (dated 18-12-2017) for it and I am no programmer, just reverse programmed (sort of) the codes which was already posted by Rahul.

@Rahul, please have a look at it in your free time and provide suggestion, how can I improve it?

 

Thanks

Regards,

Dr. Atul

edited question