Announcement

Collapse
No announcement yet.

How To Add A Profile Field To The Postbit

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • How To Add A Profile Field To The Postbit

    If you haven't already, create a new profile field in your:

    Admin CP -> User Profile Fields -> Add New User Profile Field

    Users can enter their info into the profile field by going to their User CP. To display the information in posts you need to go to your:

    Admin CP -> Styles & Templates -> Style Manager -> -> Postbit Templates -> postbit or postbit_legacy (depending on which layout you use)

    Add this code:

    Code:
    <if condition="$post['fieldX']">
        $post[fieldX]
    </if>
    Where fieldX is the field indentifier of the profile field as shown in your:

    Admin CP -> User Profile Fields -> User Profile Field Manager

    You may also wish to add a label to the profile field. For example, if the profile field is for the user's location:

    Code:
    <if condition="$post['fieldX']">
        Location: $post[fieldX]
    </if>
    The exact placement of this code depends on where you want it to show up.

  • #2
    How To Display Multiple Selection Profile Fields

    Multiple selection fields present a problem when you try to display their values in the postbit. If you use the method described above then you will get a number displayed instead of the selected values. This is due to the method that is used for storing the selected values for multiple selection fields.

    Multiple selection fields use a binary scheme to store the selected values. Each option is given a number value, like this:

    option 1 = 2^0 = 1
    option 2 = 2^1 = 2
    option 3 = 2^2 = 4
    option 4 = 2^3 = 8
    option 5 = 2^4 = 16
    option 6 = 2^5 = 32
    option 7 = 2^6 = 64
    option 8 = 2^7 = 128
    etc...

    Notice how each number is an integer power of 2. If you were to look at the stored value for a multiple selection field in natural binary format, then each bit would represent one of the options in the multiple selection field. For examlpe, if options 1 and 4 were selected then the stored value would look like this:

    Code:
            2^7    2^6    2^5    2^4    2^3    2^2    2^1    2^0
    binary #    0    0    0    0    1    0    0    1

    In this case, the stored value is 9 (the sum of the "on" bits). This is also the number that would be displayed in the postbit if you were to use the above method to display the value of this field.

    Under the current system, there is no built-in method to translate the stored value into the real value for use in the postbit. However, you can use a series of template conditionals to check each bit in the stored value and then display the appropriate code for each option:

    Code:
    <if condition="$post['fieldX'] & 1">
        CODE TO DISPLAY IF OPTION 1 IS SELECTED
    </if>
    <if condition="$post['fieldX'] & 2">
        CODE TO DISPLAY IF OPTION 2 IS SELECTED
    </if>
    <if condition="$post['fieldX'] & 4">
        CODE TO DISPLAY IF OPTION 3 IS SELECTED
    </if>
    <if condition="$post['fieldX'] & 8">
        CODE TO DISPLAY IF OPTION 4 IS SELECTED
    </if>
    <if condition="$post['fieldX'] & 16">
        CODE TO DISPLAY IF OPTION 5 IS SELECTED
    </if>
    <if condition="$post['fieldX'] & 32">
        CODE TO DISPLAY IF OPTION 6 IS SELECTED
    </if>
    <if condition="$post['fieldX'] & 64">
        CODE TO DISPLAY IF OPTION 7 IS SELECTED
    </if>
    <if condition="$post['fieldX'] & 128">
        CODE TO DISPLAY IF OPTION 8 IS SELECTED
    </if>
    The & operator is a bitwise operator. It returns a number that has the common bits turned on. So if both operands have a bit in common then that bit is turned on in the returned value. Take the first condition for example:

    Code:
    <if condition="$post['fieldX'] & 1">
        CODE TO DISPLAY IF OPTION 1 IS SELECTED
    </if>
    If the stored value is 9 (0000 1001 in binary), and we take the binary representation of 1 (0000 0001), then the condition looks like this:

    Code:
    <if condition="0000 1001 & 0000 0001">
        CODE TO DISPLAY IF OPTION 1 IS SELECTED
    </if>
    In this case, the returned value is 0000 0001, a number with the common bits turned on. A template conditional evaluates to true when the returned value is anything other than 0 (0000 0000 in binary), so if the two operands have the bit in common, that is to say if the specified option is selected in the profile field, then the condition evaluates to true and the appropriate code is displayed.
    Steve Machol, former vBulletin Customer Support Manager (and NOT retired!)
    Change CKEditor Colors to Match Style (for 4.1.4 and above)

    Steve Machol Photography


    Mankind is the only creature smart enough to know its own history, and dumb enough to ignore it.


    Comment


    • #3
      For displaying multiple selection fields, you can separate each option with commas by expanding on the previous code. Add the blue code:

      Code:
      <if condition="$comma = ''"></if>
       
      <if condition="$post['fieldX'] & 1">
          $comma CODE TO DISPLAY IF OPTION 1 IS SELECTED
          <if condition="$comma = ', '"></if>
      </if>
      <if condition="$post['fieldX'] & 2">
          $comma CODE TO DISPLAY IF OPTION 2 IS SELECTED
          <if condition="$comma = ', '"></if>
      </if>
      <if condition="$post['fieldX'] & 4">
          $comma CODE TO DISPLAY IF OPTION 3 IS SELECTED
          <if condition="$comma = ', '"></if>
      </if>
      <if condition="$post['fieldX'] & 8">
          $comma CODE TO DISPLAY IF OPTION 4 IS SELECTED
          <if condition="$comma = ', '"></if>
      </if>
      <if condition="$post['fieldX'] & 16">
          $comma CODE TO DISPLAY IF OPTION 5 IS SELECTED
          <if condition="$comma = ', '"></if>
      </if>
      <if condition="$post['fieldX'] & 32">
          $comma CODE TO DISPLAY IF OPTION 6 IS SELECTED
          <if condition="$comma = ', '"></if>
      </if>
      <if condition="$post['fieldX'] & 64">
          $comma CODE TO DISPLAY IF OPTION 7 IS SELECTED
          <if condition="$comma = ', '"></if>
      </if>
      <if condition="$post['fieldX'] & 128">
          $comma CODE TO DISPLAY IF OPTION 8 IS SELECTED
          <if condition="$comma = ', '"></if>
      </if>
      Once a field is displayed, this code turns on the comma so that the next field that is displayed will be preceded by a comma.
      Steve Machol, former vBulletin Customer Support Manager (and NOT retired!)
      Change CKEditor Colors to Match Style (for 4.1.4 and above)

      Steve Machol Photography


      Mankind is the only creature smart enough to know its own history, and dumb enough to ignore it.


      Comment


      • #4
        Appended data for 'How to add user profile field to postbit' thread.

        The following might prove a useful additional post in the 'How to add a user profile to the postbit' thread, as an expansion on the first post in that thread:

        For those interested in so doing (it seems to be a commonly requested design concept), the following code in the 'postbit' template will cause posts to display the profile field for 'Display Name' if the user has provided one; else it will display the poster's username (in the template example to follow, the X in 'fieldX' should be replaced with the number of the field in your profile fields configuration corresponding to 'Display Name', as indicated in AdminCP > User Profile Fields > User Profile Field Manager):

        Code:
        <div id="postmenu_$post[postid]">
        <if condition="$show['profile']">
        
          <if condition="$post['fieldX']">
             <a class="bigusername" href="member.php?$session[sessionurl]u=$post[userid]">$post[fieldX]</a>
          <else />
             <a class="bigusername" href="member.php?$session[sessionurl]u=$post[userid]">$post[musername]</a>
          </if>
        
           <script type="text/javascript"> vbmenu_register("postmenu_$post[postid]", true); </script>
        
        <else />
           $post[musername]
        </if>
        </div>
        Again, for the above to work, you must first go into AdminCP > User Profile Fields > Add New User Profile Field and create a profile field called 'Display Name' (or whatever else you wish to call it). Then from AdminCP > User Profile Fields > User Profile Field Manager determined which number this field bears, and use that for 'fieldX' in the above example (thus if it is field 4, use 'field4', etc.).

        With the postbit template configured in this way, you do not need to make the 'Display Name' field mandatory in the profile; if the member provides a display name, it is used on posts. If not, their username is used instead.

        Comment

        widgetinstance 262 (Related Topics) skipped due to lack of content & hide_module_if_empty option.
        Working...
        X