Adding custom fields to bbpress reply form

Referring to post "adding-custom-fields-bbpress-topic-form" with link https://wp-dreams.com/articles/2013/06/adding-custom-fields-bbpress-topic-form/, I want to add custom fields to bbpress reply form accordingly. I only altered several key words from "topic" to "reply". And I also tested more hooks to trigger the function. However, I could not show the related field after testing a test reply. Here is the code:

add_action ( 'bbp_theme_before_reply_form_content', 'bbp_extra_fields');

function bbp_extra_fields() {
   $value = get_post_meta( bbp_get_reply_id(), 'bbp_extra_field1', true);
   echo 'label for="bbp_extra_field1"Extra Field 1/labelbr';
   echo "input type='text' name='bbp_extra_field1' value='".$value."'";

   $value = get_post_meta( bbp_get_reply_id(), 'bbp_extra_field2', true);
   echo 'label for="bbp_extra_field1"Extra Field 2/labelbr';
   echo "input type='text' name='bbp_extra_field2' value='".$value."'";
}
// I can find the fields added (two input boxes)


add_action ( 'bbp_new_reply', 'bbp_save_extra_fields', 10, 1 );
add_action ( 'bbp_edit_reply', 'bbp_save_extra_fields', 10, 1 );

add_action ( 'save_post', 'bbp_save_extra_fields', 10, 1 );

function bbp_save_extra_fields($reply_id=0) {
  if (isset($_POST)  $_POST['bbp_extra_field1']!='')
    update_post_meta( $reply_id, 'bbp_extra_field1', $_POST['bbp_extra_field1'] );
  if (isset($_POST)  $_POST['bbp_extra_field1']!='')
    update_post_meta( $reply_id, 'bbp_extra_field1', $_POST['bbp_extra_field2'] );
}

//here I am not sure which hook should be used accordingly, but all four hooks do not work

add_action('bbp_template_before_replies_loop', 'bbp_show_extra_fields');
add_action('bbp_new_reply', 'bbp_show_extra_fields');
add_action('bbp_edit_reply', 'bbp_show_extra_fields');
add_action('save_post', 'bbp_show_extra_fields');

function bbp_show_extra_fields() {
  $reply_id = bbp_get_reply_id();
  $value1 = get_post_meta( $reply_id, 'bbp_extra_field1', true);
  $value2 = get_post_meta( $reply_id, 'bbp_extra_field2', true);
//finally I can not find any extra fields information after I sent the reply
  echo "Field 1: ".$value1."br";
  echo "Field 2: ".$value2."br";
}

Please kindly give suggestion. Thanks!

Topic bbpress actions metabox posts customization Wordpress

Category Web


Referring to previous post https://bbpress.org/forums/topic/add-custom-text-fields-to-reply-form/ I found "bbp_theme_after_reply_content" may be used effectively.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.