Adding custom fields to bbpress topic form

Ernest Marcinko Blog, Tutorials, Wordpress 26 Comments

Tutorial level: Medium

Requirements

  • Knowledge of: PHP, HTML, CSS
  • Basic knowledge of WordPress actions and filters
  • BBPress and WordPress 3.5+
  • BBPress actions list, here is one

Tutorial Content

  • Creating a custom field in the bbpress topic form
  • Processing & Saving the custom field values
  • Displaying custom values in the content

As some of you may noticed, our support forum’s new ticket form has some custom fields included. I have searched through the web for tutorials/plugins before creating the new support form, but unfortunately there is almost nothing out there. I knew I will have to code a lot, even if I find some tutorials. Actually, this is not hard at all, since bbpress has a bunch of actions and filters integrated, which helps us to modify the look and feel of it, without actually touching the bbpress code.

Creating a custom field in the bbpress topic form

Open up your themes functions.php file, or create a new plugin to start from scratch. In the bbpress default template I have allocated an action called ‘bbp_theme_before_topic_form_content‘. We will create a function, which displays the new input fields and hook it to that action, so the new input field will display right after the title, before the reply content textarea.

add_action ( 'bbp_theme_before_topic_form_content', 'bbp_extra_fields');
function bbp_extra_fields() {
   $value = get_post_meta( bbp_get_topic_id(), 'bbp_extra_field1', true);
   echo '<label for="bbp_extra_field1">Extra Field 1</label><br>';
   echo "<input type='text' name='bbp_extra_field1' value='".$value."'>";

   $value = get_post_meta( bbp_get_topic_id(), 'bbp_extra_field2', true);
   echo '<label for="bbp_extra_field1">Extra Field 2</label><br>';
   echo "<input type='text' name='bbp_extra_field2' value='".$value."'>";
}

extra1

As you can see, there is an extra $value variable involved for getting the default values, however it is not set yet. We will do it in the next step.

Basically you can add as many fields as you want. You can even use select boxes, hidden fields and anything you want to.

Processing & Saving the custom field values

Ok, we have the fields displayed on the new topic form, but they are useless to us, since we cannot save the values. No worries, just a few lines, and the custom values are persistent.

BBPress offers actions before&after saving/editing new topics. These actions are:
do_action( ‘bbp_new_topic’, $topic_id, $forum_id, $anonymous_data, $topic_author ); and
do_action( ‘bbp_edit_topic’, $topic_id, $forum_id, $anonymous_data, $topic_author );

We need to create function, which saves the values after saving and hook that function to these 2 actions.

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 (isset($_POST) && $_POST['bbp_extra_field1']!='')
    update_post_meta( $topic_id, 'bbp_extra_field1', $_POST['bbp_extra_field1'] );
  if (isset($_POST) && $_POST['bbp_extra_field1']!='')
    update_post_meta( $topic_id, 'bbp_extra_field1', $_POST['bbp_extra_field2'] );
}

 

As you can see, our function accepts only 1 parameter, the topic id, saves the values into a new post meta and it’s done. We can access these values anywhere on the forums if we know the topic id!

Displaying custom values in the content

I’m pretty sure, that you want to display the newly created fields somwhere. You can do it several ways, I will show you the best one. The ‘bbp_template_before_replies_loop‘ action runs before the topic replies are displayed, this is just what we need. This actions has no parameters whatsoever, and we need the topic id to display the data, which is stored in the database. No worries, bbpress offers the bbp_get_topic_id() function inside the post loop.

add_action('bbp_template_before_replies_loop', 'bbp_show_extra_fields');
function bbp_show_extra_fields() {
  $topic_id = bbp_get_topic_id();
  $value1 = get_post_meta( $topic_id, 'bbp_extra_field1', true);
  $value2 = get_post_meta( $topic_id, 'bbp_extra_field2', true);
  echo "Field 1: ".$value1."<br>";
  echo "Field 2: ".$value2."<br>";
}

After adding all of these lines to the functions.php, create a new topic, and you should see the contents just below the title!

extra2

I hope you enjoyed this tutorial! If you have questions, feel free to ask in the comment section.

Comments 26

  1. kolja
    kolja

    Hi Ernest

    Thanks for this good idea!

    We want to realize a calendar function with it, because no calendar plugin works with bbpress like we want to.

    The only thing we need now, is a page where all topics with a custom field (the calendar date) are displayed.

    Can you help us with it?

    Greetings outa Germany

    kolja

  2. BryanPrice
    BryanPrice

    Hey there,

    I was able to integrate custom fields for a new topic form based on your tutorial. Thank you very much for sharing the information.

    I was wondering if there was a way to only call my new topic form for specific BuddyPress groups instead of globally.

    It would give the users of our site the best of both worlds – our custom form for specific groups to be used for specific purposes and the regular ‘new topic’ form for everything else.

    Thanks for the info/direction.

    1. Ernest Marcinko Post
      Author
      Ernest Marcinko

      Hi Phil!

      I have to agree and disagree as well:

      • The wordpress standard search will indeed not work with custom fields.
      • However one of my products called Ajax search pro will and can search selected custom fields, even with custom post types like bbpress posts.

      Best,
      Ernest

    1. Ernest Marcinko Post
      Author
      1. Bill Robbins

        You’ll still want to validate and sanitize the inputs here. The update_post_meta() function only runs php’s stripslashes which unescapes text (https://codex.wordpress.org/Function_Reference/update_post_meta and http://www.php.net/stripslashes). It just takes out single slashes from the input.

        There is an article at WP VIP that can show a few examples of validating and sanitizing data before it’s run through update_post_meta() — https://vip.wordpress.com/documentation/best-practices/security/validating-sanitizing-escaping/

        Thanks for writing this tutorial too. It’s just what I needed.

        1. Ernest Marcinko Post
          Author
  3. janfeng

    Thank you very much !

    If want to always display behind topics title, How ?!

    For example :

    Loop-topics
    Post title
    The browser title
    ……..

  4. Vinay

    Hi Ernest, how do I add a “drop-down list” field as “extra field” instead of a simple text field? Thanks in advance.

  5. jackie

    Excellent tutorial, just a quick question, how can I add those fields only in one specific forum? I tried everything, but I still can’t figure it out.

  6. Samuel

    Thanks Ernest!! this has been quite helpful!!

    Just a little note, in the saving function, for the second field (Field 2), you are saving it with the first field’s meta:

    // this
    if (isset($_POST) && $_POST[‘bbp_extra_field1’]!=”)
    update_post_meta( $topic_id, ‘bbp_extra_field1’, $_POST[‘bbp_extra_field2’] );
    // should be
    if (isset($_POST) && $_POST[‘bbp_extra_field2’]!=”)
    update_post_meta( $topic_id, ‘bbp_extra_field2’, $_POST[‘bbp_extra_field2’] );

    Thank you once again!!

  7. Mike Keelback

    Quote:”Basically you can add as many fields as you want. You can even use select boxes, hidden fields and anything you want to.”. Could you post an example like you did above for one drop down extra field.
    And nearly more important for me: Is there a way to show the value of those extra fields in the Topic list next to Voices, Posts and freshness? If i had only one field showing up in that list, would save my day, lol.
    Thanks so much for that Manual, i was searching very long time to solve that problem

    1. Samuel

      Hey Mac,

      You can use `bbp_add_error()` function for this, bbPress will not process a topic/reply unless all errors are cleared, and to register a custom error, the best hook out there is `bbp_new_topic_pre_extras`:

      add_action(‘bbp_new_topic_pre_extras’, function( $forum_id ) {
      if ( empty( $_POST[‘bbp_extra_field1’] ) && !is_admin() ) {
      return bbp_add_error(
      ‘bbp_extra_field1’,
      ERROR: Your topic needs an bbp_extra_field1, please input a field value for this.’
      );
      }
      });

  8. vinzent

    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.

  9. Ben

    Hi.

    Is it possible to display it only on a specific forum?
    I have seen that ACF enables to display fields on all topic or all forums but not on specific ones..

  10. Clive

    Hi,

    Not sure if this is still a relevant topic but I hope you can tell me, I have only made the changes to my functions.php so far, but I cannot see any change to my new topic form, am I right in thinking I should ?
    Clive

  11. arcel

    I’m using bbPress for the forum on my site.

    On the front end, users should be able to submit a topic on a forum, when they try, they get a “A name is required for this term.” error, I have already reset the forum, but still has the same thing.
    Also the despite that message, the topic will appear in the WP admin dashboard under Topics, but will never show in the forum on the front end, I would like the topic created by users on the front end to appear immediately in the front end as well, any idea of code I can add to the functions.php of the theme or anything I can do ?

Leave a Reply

Your email address will not be published.