Categories::ap_question_form_fields( array $form )

Description #

Add category field in ask form.

Parameters #

  • $form
    array (Required) Ask form arguments.

Changelog #

VersionDescription
4.1.0Introduced.

Source #

File: addons/categories/categories.php

	public function ap_question_form_fields( $form ) {
		if ( wp_count_terms( 'question_category' ) == 0 ) { // phpcs:ignore
			return $form;
		}

		$editing_id  = ap_sanitize_unslash( 'id', 'r' );
		$category_id = ap_sanitize_unslash( 'category', 'r' );

		$form['fields']['category'] = array(
			'label'    => __( 'Category', 'anspress-question-answer' ),
			'desc'     => __( 'Select a topic that best fits your question.', 'anspress-question-answer' ),
			'type'     => 'select',
			'options'  => 'terms',
			'order'    => 2,
			'validate' => 'required,not_zero',
		);

		// Add value when editing post.
		if ( ! empty( $editing_id ) ) {
			$categories = get_the_terms( $editing_id, 'question_category' );

			if ( $categories ) {
				$form['fields']['category']['value'] = $categories[0]->term_id;
			}
		} elseif ( ! empty( $category_id ) ) {
			$form['fields']['category']['value'] = (int) $category_id;
		}

		return $form;
	}

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Add your comment