Created
May 21, 2015 11:48
-
-
Save rahularyan/0b37680c1a24c1e78baf to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* add category field in ask form | |
* @param array $validate | |
* @return void | |
*/ | |
function ask_from_category_field($args, $editing){ | |
if(wp_count_terms('question_category') == 0) | |
return $args; | |
global $editing_post; | |
if($editing){ | |
$category = get_the_terms( $editing_post->ID, 'question_category' ); | |
$catgeory = $category[0]->term_id; | |
} | |
$args['fields'][] = array( | |
'name' => 'category', | |
'label' => __('Category', 'categories_for_anspress'), | |
'type' => 'taxonomy_select', | |
'value' => ( $editing ? $catgeory : sanitize_text_field(@$_POST['category'] )) , | |
'taxonomy' => 'question_category', | |
'orderby' => ap_opt('form_category_orderby'), | |
'desc' => __('Select a topic that best fits your question', 'categories_for_anspress'), | |
'order' => 6 | |
); | |
return $args; | |
} | |
add_action('ap_ask_form_fields', 'ask_from_category_field', 10, 2); | |
/** | |
* add category in validation field | |
* @param array $fields | |
* @return array | |
* @since 1.0 | |
*/ | |
function ap_ask_fields_validation($args){ | |
if(wp_count_terms('question_category') == 0) | |
return $args; | |
$args['category'] = array( | |
'sanitize' => array('only_int'), | |
'validate' => array('required'), | |
); | |
return $args; | |
} | |
add_action('ap_ask_fields_validation', 'ap_ask_fields_validation'); | |
/** | |
* Things to do after creating or updating question | |
* @param int $post_id | |
* @param object $post | |
* @return void | |
* @since 1.0 | |
*/ | |
function after_new_question($post_id, $post) | |
{ | |
global $validate; | |
if(empty($validate)) | |
return; | |
$fields = $validate->get_sanitized_fields(); | |
if(isset($fields['category'])) | |
$category = wp_set_post_terms( $post_id, $fields['category'], 'question_category' ); | |
} | |
add_action('ap_processed_new_question', 'after_new_question', 0, 2 ); | |
add_action('ap_processed_update_question', 'after_new_question', 0, 2 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment