Skip to content

Instantly share code, notes, and snippets.

@atultiwari
Created December 18, 2017 11:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save atultiwari/29ddff5fa59c97153061d3068db1531f to your computer and use it in GitHub Desktop.
Save atultiwari/29ddff5fa59c97153061d3068db1531f to your computer and use it in GitHub Desktop.
<?php
/**
* Function created by - Dr. Atul
* Adapted from Anspress by Rahul Aryan
**/
function AddQuestionToAnsPress($question_title, $question_content, $answer_content, $anspress_category_id, $anspress_tags_id) {
require('../wp-load.php');
$question_title = strip_tags($question_title);
$form = anspress()->get_form( 'question' );
$values = array(
'post_title' => $question_title,
'post_content' => $question_content,
'is_private' => false,
'category' => $anspress_category_id,
'tags' => $anspress_tags_id,
);
@$form->set_values( $values );
$question_post_id = AP_Form_Hooks::submit_question_form( true );
if ( ! is_wp_error( $question_post_id ) ) {
$form = anspress()->get_form( 'question' );
$values = $form->get_values();
$qameta = array(
'last_updated' => current_time( 'mysql' ),
'answers' => ap_count_published_answers( $question_post_id ),
'anonymous_name' => sanitize_text_field( $values['anonymous_name']['value'] ),
'selected' => false,
'views' => 100,
'closed' => false,
'comments' => 0,
'featured' => false,
'votes_up' => 5,
'votes_down' => 0,
'subscribers' => 0,
'flags' => 0,
);
$question_post = get_post( $question_post_id );
ap_insert_qameta( $question_post_id, $qameta );
do_action( 'ap_processed_new_question', $question_post_id, $question_post );
ap_update_qameta_terms( $question_post_id );
$activity_type = 'new_q';
ap_activity_add( array(
'q_id' => $question_post_id,
'action' => $activity_type,
) );
$question_permalink = get_permalink($question_post_id);
}
$form = anspress()->get_form( 'answer' );
$values = array(
'post_content' => $answer_content,
'post_type' => 'answer',
'post_title' => $question_post_id,
'post_status' => 'publish',
'comment_status' => 'open',
'question_id' => $question_post_id,
'post_author' => get_current_user_id(),
'post_name' => $question_post_id,
);
@$form->set_values( $values );
//$answer_post_id = AP_Form_Hooks::submit_answer_form( true );
$answer_post_id = at_submit_answer_form( true, $question_post_id );
if ( ! is_wp_error( $answer_post_id ) ) {
$answer_post = get_post( $answer_post_id );
$activity_type = 'new_answer';
$qameta = array(
'last_updated' => current_time( 'mysql' ),
'activities' => array(
'type' => $activity_type,
'user_id' => $answer_post->post_author,
'date' => current_time( 'mysql' ),
),
);
ap_insert_qameta( $answer_post_id, $qameta );
do_action( 'ap_processed_new_answer', $answer_post_id, $answer_post );
ap_update_qameta_terms( $answer_post_id );
$activity_type = 'new_a';
ap_activity_add( array(
'q_id' => $answer_post->post_parent,
'a_id' => $answer_post_id,
'action' => $activity_type,
) );
ap_set_selected_answer( $question_post_id, $answer_post_id );
ap_update_answers_count( $answer_post->post_parent, 1, true );
} else {
$error_string = $answer_post_id->get_error_message();
echo '<div id="message" class="error"><p>' . $error_string . '</p></div>';
}
return array('anspress_link' => $question_permalink, 'question_post_id' => $question_post_id, 'answer_post_id' => $answer_post_id);
}
?>
@levikapitany
Copy link

Hi there!
I'd like to ask if this code sample will work with the latest version of AnsPress, or do I need to modify the core files in the plugin?
Thank you,
Levi

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment