All the code below need to be placed in function.php on your theme.

Create the additional fields


add_action ( 'bbp_theme_before_topic_form_content', 'ajde_bbp_extra_fields');
function ajde_bbp_extra_fields() {
$value = get_post_meta( bbp_get_topic_id(), 'ajde_bbpress_field_1', true);
echo '<p><label for="ajde_bbpress_field_1">Direct URL to where the issue happens *</label><br>';
echo "<input style='width:100%; box-sizing: border-box;' type='text' name='ajde_bbpress_field_1' value='".$value."'></p>";

$value = get_post_meta( bbp_get_topic_id(), 'ajde_bbpress_field_2', true);
echo '<p><label for="ajde_bbpress_field_1">WP-admin login access to your site (Only we will see this info)</label><br>';
echo "<input style='width:100%; box-sizing: border-box;' type='text' name='ajde_bbpress_field_2' value='".$value."'></p>";
}

Above code will add 2 new fields in the new topic form like below. (FYI I have modified the new topics form and CSS styled it to look nicer than defauly bbpress ugly look)

Capture

 

Save the field values

Code below will save the values entered in the fields as post meta fields for the topic post.


add_action ( 'bbp_new_topic', 'bbp_save_extra_fields', 10, 1 );
add_action ( 'bbp_edit_topic', 'bbp_save_extra_fields', 10, 1 );
function bbp_save_extra_fields($topic_id=0) {
if (!empty($_POST['ajde_bbpress_field_1']))
update_post_meta( $topic_id, 'ajde_bbpress_field_1', $_POST['ajde_bbpress_field_1'] );
if (!empty($_POST['ajde_bbpress_field_2']))
update_post_meta( $topic_id, 'ajde_bbpress_field_2', $_POST['ajde_bbpress_field_2'] );
}

 

Show the saved values

In the example code below I am only showing the data values to users with ID 1 and 2. You can remove the if statement for user ID check to show these values to everyone.


add_action('bbp_template_before_replies_loop', 'bbp_show_extra_fields');
function bbp_show_extra_fields() {

$user_id = get_current_user_id();

// restrict access to this info to only allowed users of site
if(!empty($user_id) && ($user_id==1 || $user_id==2)){

global $post;
$topic_id = $post->ID;
//print_r(get_post_custom($topic_id));

$topic_id = bbp_get_topic_id();
$value1 = get_post_meta( $topic_id, 'ajde_bbpress_field_1', true);
$value2 = get_post_meta( $topic_id, 'ajde_bbpress_field_2', true);

echo "<div class='topic_extra_data' style='background-color:#ECF0F2;border-radius: 5px;padding: 10px; margin-top:10px'>
<p style='margin-bottom:5px;'><b>Site URL:</b> ".$value1."</p>
<p style='margin-bottom:5px;'><b>WP-admin Info:</b> ".$value2."</p>
</div>";
}
}

Final view with the additional fields showing in topics page.
Capture


Categories

bbpress, code snippet, Custom Fields

One response to “Adding custom fields to bbpress new topic form”

  1. vinzent says:

    Hi.
    Thank you very much good information.

    The one thing I need help.
    I want to see the additional information field on another page.
    For example,
    1. In the list of topics bbpress.
    or
    2. In the blog grid view screen.

    If you know, please tell me the available commands.

    Thank you.

Leave a Reply

Your email address will not be published. Required fields are marked *