Welcome to the Question2Answer Q&A. There's also a demo if you just want to try it out.
+2 votes
in Q2A Core by
I would to display a question and allow users to vote on a regular page on my wordpress site. I've worked out how to display the question but how can I code in allowing the voting to happen?

Where can I find the relevant code for the voting to take place?

1 Answer

+1 vote

Voting is done with AJAX. Take a look at the qa_vote_click function in qa-content/qa-page.js

You can trigger that from an input element with a name property, for example the upvote on this page is

<input title="Click to vote up" name="vote_57443_1_q57443" onclick="return qa_vote_click(this);" type="submit" value="+" class="qa-vote-first-button qa-vote-up-button">

In "vote_57443_1_q57443" you have the postid (57443), the vote (1 for upvote, -1 for downvote) and an achor link (q57443) which is used as a hook to display any error message returned.

A lot of this is ingrained into how Q2A itself works, for example it returns the HTML needed for the theme. So to do this from outside the code, you may find it simpler to make your own AJAX request. You'd make an AJAX POST request to the Q2A root URL (e.g. yoursite.com/qa/) with the following parameters:

qa: 'ajax',
qa_operation: 'vote',
qa_root: /* Q2A root URL */,
qa_request: /* current page */,
postid: /* the post ID */,
vote: /* 1 or -1 */,
code: /* security code from qa_get_form_security_code() function */

I've created a plugin to run this. Is there a way to include the necessary q2a files and then I can run this from within q2a? Which files would I need to include? If not how would I set the 'postid' I am listing all questions related to a specific category on the page, I would like users to vote for these questions.

Welcome to the Q&A site for Question2Answer.

If you have a question about Q2A, please ask here, in English.

To report a bug, please create a new issue on Github or ask a question here with the bug tag.

If you just want to try Q2A, please use the demo site.