Custom fields

1.21K viewsCorecustom fields
0

Hi,
I have trouble adding a custom field to the Question and reply form.
I have successfully added (and modified) the field to the question form. 

add_filter( 'buddypages_groups_get_is_admin', 'pluginizehelp_include_hidden_groups' );
/**
 * Add custom fields to AnsPress question form.
 * 
 * This hook will add two new fields in ask form.
 *
 * @param array $form Form arguments.
 * @return void
 */
function my_custom_question_field( $form ) {
     // Nickname question field.
  $form['fields']['anonymous_name'] = array(
    'label'       => __( 'Name', 'anspress-question-answer' ),
  'validate'    => 'required,badwords',
    'order'      => 2,
    'attr'       => array(
     'placeholder' => __( 'Kaldenavn' ),
    ),
  );
     // Age question field.
  $form['fields']['age'] = array(
     'type'          => 'input',
 'subtype'       => 'number',
      'attr'       => array(
     'placeholder' => __( 'Alder' ),
    ),
    'label'       => __( 'Your age', 'anspress-question-answer' ),
  'validate'    => 'required,badwords',
    'order'      => 2,
  );
   // editing question field.
  $form['fields']['post_content'] = array(
    'type'        => 'textarea',
    'label'       => __( 'Description', 'anspress-question-answer' ),
        'attr'       => array(
     'placeholder' => __( 'Spørgsmål' ),
    ),
  'min_length'  => ap_opt( 'minimum_question_length' ),
  'validate'    => 'required,min_string_length,badwords',
  );
     return $form;
}
add_filter( 'ap_question_form_fields', 'my_custom_question_field' );


But I cannot figure out how to save it.

  1. I am not sure where to expect it to be saved? Should it be saved to the wp_ap_qameta table to the “fields” column just like anonymous_field is? Or should i register an actual custom field? If so – how?
  2. How do I save it? I have tried the code bellow, but even with all the stuff commented out it is causing a 500 error to an AJAX call when submitting the question. It gets submitted but I am not redirected to the question page.
  3. I see no documentation on how to retrieve this custom field but it kind of depends on the answers to the previous 2 questions.

//----------- S A V I N G --------
function after_new_question($post_id, $post)
{
     $form = anspress()->get_form( 'question_form' );
    // Get all sanitized values.
    $values = $form->get_values();
    // Check nonce and is valid form.
     /*
    if ( ! $form->is_submitted() ) {
      echo __( 'Holla, you trying to cheat!' );
      return;
    }*/
   // update_post_meta( $post_id, 'age', $values['age']['value'] );
         }
add_action('ap_processed_new_question', 'after_new_question', 0, 2 );
add_action('ap_processed_update_question', 'after_new_question', 0, 2 );

I see this questin asked multiple times, but could’t work out the solution as some responses seem to be outdated or just pointing to this article https://anspress.io/resources/faq/anspress-form-and-validation-api/ that does not contain full solution.

Asked question