vb.org Archive

vb.org Archive (https://vborg.vbsupport.ru/index.php)
-   vBulletin 4 Articles (https://vborg.vbsupport.ru/forumdisplay.php?f=242)
-   -   Using your User Profile Fields in your postbit templates (w/ all plugin method) (https://vborg.vbsupport.ru/showthread.php?t=250418)

Lynne 09-12-2010 10:00 PM

Using your User Profile Fields in your postbit templates (w/ all plugin method)
 
This article is written with the assumption that you have already created your own User Profile Fields in the User Profile Field Manager. If you need help with this, you may read about it in the manual here - http://www.vbulletin.com/docs/html/profile

For the Purposes of this article, I will be referring to "fieldx" as the name of your profile field. Your real name will be found in the User Profile Manager in the "Name" column - field1, field2, etc.

Adding the Profile Field to your postbit (or postbit_legacy) template

For Single-Line Text Box, Multiple-Line Text Box, Single-Selection Radio Buttons, and Single-Selection Menu

Method 1
- Modifying the Template
Open the postbit or postbit_legacy template and find the area you want to add it to. For instance, to add it right after the user post count, find this:
Code:

<dt>{vb:rawphrase 'posts'}</dt> <dd>{vb:raw post.posts}</dd>
And then, add this underneath:
Code:

<vb:if condition="$post['fieldx']"><dt>My FieldX</dt> <dd>{vb:raw post.fieldx}</dd></vb:if>
Change "fieldx" to your actual field name and the green field to whatever you want. Notice the html I used is similar to the html used for the text above me new field.
Method 2 - Using a template_hook
I have always found it easier to just use the existing $template_hooks in the template and write a plugin to add these. In this case, there is a $template_hook right where we want it, after the user post count:
Code:

{vb:raw template_hook.postbit_userinfo_right_after_posts}
So, we would create a plugin with these specifications:
? hook location - postbit_display_complete
? Title - Add User Profile Fields to Postbit Templates
? Plugin is Active - Yes
? Plugin PHP Code -
Code:

if ($post['fieldx'])
{
    $template_hook['postbit_userinfo_right_after_posts'] .= '<dt>My FieldX</dt> <dd>' .$post[fieldx]. '</dd>';
}

Change "fieldx" to your actual field name and the green field to whatever you want. Again, notice the html I used is similar to the html used for the text above me new field. Whichever template_hook you use, you should look at the template to see what sort of html is being used around the hook so that you may use similar, proper html also.
For Multiple-Selection Menu and Multiple-Selection Checkbox

It is a bit more complicated for these two types of selections since the options selected are stored as a binary number. If you just display the field using the method above, you will get a number, not a list of options selected. So, in order to use these types of fields, you will have to use the method below. Here is a thead that will explain the binary scheme - http://www.vbulletin.com/forum/showt...To-The-Postbit

Method 1
- Modifying the Template
Open the template and find the area you want to add it to. For instance, to add it right after the user post count, find this:
Code:

<dt>{vb:rawphrase 'posts'}</dt> <dd>{vb:raw  post.posts}</dd>
And then add this underneath (this is assuming the field has 5 options):
Code:

<vb:if condition="$post['fieldx']"><dt>My FieldX</dt><dd>
<vb:if condition="$post['fieldx'] & 1">Your 1rst option</vb:if>
<vb:if condition="$post['fieldx'] & 2">Your 2nd option</vb:if>
<vb:if condition="$post['fieldx'] & 4">Your 3rd option</vb:if>
<vb:if condition="$post['fieldx'] & 8">Your 4th option</vb:if>
<vb:if condition="$post['fieldx'] & 16">Your 5th option</vb:if>
</dd>
</vb:if>

Change "fieldx" to your actual field name and the green field to whatever you want. Notice the html I used is similar to the html used for the text above me new field.
Method 2 - Using a template_hook
Find the template_hook you want to use. In this case, there is a $template_hook right where we want it, after the user post count:
Code:

{vb:raw template_hook.postbit_userinfo_right_after_posts}
So, we would create a plugin with these specifications:
? hook location - postbit_display_complete
? Title - Add User Profile Fields to Postbit Templates
? Plugin is Active - Yes
? Plugin PHP Code -
Code:

if ($post['fieldx'])
{
    $template_hook['postbit_userinfo_right_after_posts'] .= '<dt>My FieldX</dt> <dd>';
   
    if ($post['fieldx'] & 1)  $template_hook['postbit_userinfo_right_after_posts'] .= 'Your 1rst option';
    if ($post['fieldx'] & 2)  $template_hook['postbit_userinfo_right_after_posts'] .= 'Your 2nd option';
    if ($post['fieldx'] & 4)  $template_hook['postbit_userinfo_right_after_posts'] .= 'Your 3rd option';
    if ($post['fieldx'] & 8)  $template_hook['postbit_userinfo_right_after_posts'] .= 'Your 4th option';
    if ($post['fieldx'] & 16)  $template_hook['postbit_userinfo_right_after_posts'] .= 'Your 5th option';
   
    $template_hook['postbit_userinfo_right_after_posts'] .= '</dd>';
}

Change "fieldx" to your actual field name and the green field to whatever you want. Again, notice the html I used is similar to the html used for the text above me new field. Whichever template_hook you use, you should look at the template to see what sort of html is being used around the hook so that you may use similar, proper html also.
More Advanced

If you like everything to be 'automatic' - meaning you don't have to supply any text at all, just use everything already stored in the database - then you can use a few of plugins to do the work for you.

Plugin 1
- This plugin is used to get the phrasegroup "cprofilefield" added for use in the page so that you may use $vbphrase[fieldx_title] to get the Profile Field Title you entered in the User Profile Field Manager.
? hook location - init_startup
? Title - Add User Profile Fields to Postbit Templates - 1
? Plugin is Active - Yes
? Plugin PHP Code -
Code:

if (THIS_SCRIPT == 'showthread') $GLOBALS['phrasegroups'][] = 'cprofilefield';
Plugin 2 - This plugin is used to get the fields you defined for the profile in the User Profile Field Manager.
? hook location - showthread_postbit_create
? Title - Add User Profile Fields to Postbit Templates - 2
? Plugin is Active - Yes
? Plugin PHP Code -
Code:

if (THIS_SCRIPT == 'showthread') {
    $profilefieldx =  $vbulletin->db->query_first("SELECT profilefieldid, data, type  FROM " . TABLE_PREFIX . "profilefield WHERE profilefieldid = x");
    $post['profilefieldx'] = $profilefieldx;
}

Plugin 3 - This plugin simply spits the data out into the postbit using the template_hook and using the phrase for the Title and the Options are all spit out with commas between them (if needed) into the postbit.
? hook location - postbit_display_complete
? Title - Add User Profile Fields to Postbit Templates - 3
? Plugin is Active - Yes
? Plugin PHP Code -
Code:

// for single-line fields - using fieldy
if ($post['fieldy']) {
    $template_hook['postbit_userinfo_right_after_posts'] .= '<dt>' .$vbphrase[fieldy_title]. '</dt> <dd>' .$post[fieldy]. '</dd>';
 }

// for multiple-selection fields - using filedx
if ($post['fieldx'])
{
    $fieldarrayx = $post['profilefieldx'];
    fetch_profilefield_display($fieldarrayx, $post['fieldx']);
    $template_hook['postbit_userinfo_right_after_posts'] .= '<dt>' .$vbphrase[fieldx_title]. '</dt> <dd>' .$fieldarrayx['value']. '</dd>';
}

Change "fieldx" to your actual field name and the orange, purple, and brown fields should 'match-up' and have the same names.

Dave-M 09-14-2010 02:09 PM

Hi Lynne, thanks for this, brilliant :)

I wonder if it would be possible for you to implement a dropdown for this, so that it's like wired1's mod? His has broken CSS at the moment and there's no sign of him being able to fix it soon, it would be fantastic if you could make something similar.

https://vborg.vbsupport.ru/showthread.php?t=248651

Lynne 09-14-2010 02:15 PM

This is an article, not a modification. You may do whatever you want with the information found in here.

Lynne 09-20-2010 02:00 PM

Quote:

Originally Posted by lightbox (Post 2100959)
Thanks a lot for this article.

Is there a way to use specific User Profile Fields in vbcms article pages (Template vbcms_content_article_page)?

Cheers,
Martin

I'm sure there is, but I didn't cover that in this article. This article is just for the postbit(_legacy) template.

svoophod 09-22-2010 09:30 PM

thanks so much, this is great!

COL NIL SATIS 09-26-2010 05:13 PM

Thanks lynne..this is great!!!! top advice ..

djr 10-15-2010 10:43 PM

Hi Lynne,

Is it easy to backport this to vB 3.8.6? Besides the obvious variable name changes, would the plugin solution work for vB 3.8.6 as well? (meaning: does vB 3.8.6 have the same hooks present?). Or does it need to be completely rewritten?

I'd love to have this a plugin solution for our vB 3.8.6 board, since it's getting tiresome to redo all the template changes after every upgrade (plus I like the plugin solution better)

Thanks for an excellent article! Much appreciated.

Lynne 10-15-2010 11:00 PM

The method of calling the phrasegroup may have been different in 3.8, so you would have to change that. Other than that, you'd just have to change the html you are outputting as <li> wasn't used in 3.8 either. However, I've never tried it on my 3.8 board, so I can't say for sure.

voglermc 11-04-2010 11:10 AM

<a href="album.php?{vb:raw session.sessionurl}u={vb:raw post.userid}" title="View {vb:raw post.username}'s Album" rel="nofollow">View {vb:raw post.username}'s Album</a>

Lynne 11-04-2010 01:40 PM

You don't need user profile fields to link to album pages (unless you are doing something different?). I'm sure there is a mod or a thread about what you want to do if you are talking about just a plain link that has nothing to do with profile fields.

voglermc 11-04-2010 02:02 PM

Code:

<a href="album.php?{vb:raw session.sessionurl}u={vb:raw post.userid}" title="View {vb:raw post.username}'s Album" rel="nofollow">View {vb:raw post.username}'s Album</a>

Lynne 11-04-2010 02:29 PM

Then you should post in the main forums about it, not in some random article about the profile fields.

Black Tiger 11-30-2010 07:32 PM

Looks very interesting. You told me this was possible to make compatible for v3.8.x by changing some html code.

Could you or somebody else tell me what exactly to change? I can read a bit of php, but don't know what to do with it, so I would really need a "look for this and replace with that" solution. It has no hurry though.;)

Thanks in advance!

Lynne 11-30-2010 09:03 PM

Did you see this for 3.8? How To Add Custom Profile Field Information To The Postbit

The only thing I think she doesn't cover is doing it all via pluginand it isn't much different at all for 3.8.

Plugin 1
? hook location - init_startup
? Title - Add User Profile Fields to Postbit Templates - 1
? Plugin is Active - Yes
? Plugin PHP Code -
Code:

if (THIS_SCRIPT == 'showthread') $phrasegroups[] = 'cprofilefield';
Plugin 2
? hook location - showthread_postbit_create
? Title - Add User Profile Fields to Postbit Templates - 2
? Plugin is Active - Yes
? Plugin PHP Code -

Code:

if (THIS_SCRIPT == 'showthread') {
    $profilefieldx =  $vbulletin->db->query_first("SELECT profilefieldid, data, type  FROM " . TABLE_PREFIX . "profilefield WHERE profilefieldid = x");
    $post['profilefieldx'] = $profilefieldx;
}

Plugin 3
? hook location - postbit_display_complete
? Title - Add User Profile Fields to Postbit Templates - 3
? Plugin is Active - Yes
? Plugin PHP Code -
Code:

// for single-line fields - using fieldy
if ($post['fieldy']) {
    $template_hook['postbit_userinfo_right_after_posts'] .= '<div>' .$vbphrase[fieldy_title]. ': ' .$post[fieldy]. '</div>';
 }

// for multiple-selection fields - using filedx
if ($post['fieldx'])
{
    $fieldarrayx = $post['profilefieldx'];
    fetch_profilefield_display($fieldarrayx, $post['fieldx']);
    $template_hook['postbit_userinfo_right_after_posts'] .= '<div>' .$vbphrase[fieldx_title]. ': ' .$fieldarrayx['value']. '</div>';
}


Black Tiger 11-30-2010 09:16 PM

Yep I have seen that, but she left out the Multiple Selection Menus (like in my post about the comma at vbulletin.com yesterday) and that is just what we needed. That part seems covered now in plugin 3 if I understand everything correctly.

Thank you very much for your time explaining it, we are going to try it.

Lynne 11-30-2010 10:06 PM

You need to do plugin 1, 2, and 3, not just 3.

Black Tiger 11-30-2010 10:20 PM

Yep, I understood, but thank you for making sure, because I see in my reply I was not clear about me understanding that we needed all 3.;)

Bundle 12-12-2010 03:17 PM

Is there a way to make this only display if the user has filled in the profile field? e.g. if I tried to make a custom profile field about favourite food, is there a way to stop "Favourite Food: " appearing if the field is blank?

Cheers.

Lynne 12-12-2010 06:55 PM

The if condition around it should make it so it doesn't show if it doesn't exist.

sulasno 01-08-2011 11:15 PM

thanks for the link to this article

definitely a keeper

stl7997 01-11-2011 02:53 AM

1 Attachment(s)
Thank you so much for this article! I was wondering where I need to add a break (see picture) to the code in the template. I would like a break to be done after the 'AE Assignments:' so that everything appears on the line below it.

Thanks in advance!

Lynne 01-11-2011 03:02 AM

Quote:

Originally Posted by stl7997 (Post 2147374)
Thank you so much for this article! I was wondering where I need to add a break (see picture) to the code in the template. I would like a break to be done after the 'AE Assignments:' so that everything appears on the line below it.

Thanks in advance!

Can't you just add a <br /> where you want a break?

stl7997 01-11-2011 09:32 AM

Quote:

Originally Posted by Lynne (Post 2147377)
Can't you just add a <br /> where you want a break?

Yes that is exactly what I want to do, however I'm a complete noob to PHP. I've tried adding the <br /> in different places to the code but cannot get the desired results.

Where would I add the <br /> in this code? Or is this even the right place to place it?

Sorry for such rookie questions :o

PHP Code:

// for single-line fields - using fieldy
if ($post['fieldy']) {
     
$template_hook['postbit_userinfo_right_after_posts'] .= '<dt>' .$vbphrase[fieldy_title]. '</dt> <dd>' .$post[fieldy]. '</dd>';
 }

// for multiple-selection fields - using filedx
if ($post['fieldx'])
{
    
$fieldarrayx $post['profilefieldx'];
    
fetch_profilefield_display($fieldarrayx$post['fieldx']);
    
$template_hook['postbit_userinfo_right_after_posts'] .= '<dt>' .$vbphrase[fieldx_title]. '</dt> <dd>' .$fieldarrayx['value']. '</dd>'



Lynne 01-11-2011 08:16 PM

Put it right after the </dt> and before the <dd> for the field you are adding.

stl7997 01-12-2011 02:20 AM

Quote:

Originally Posted by Lynne (Post 2147698)
Put it right after the </dt> and before the <dd> for the field you are adding.

:up::D

Thanks Lynne!

Sarcoth 01-20-2011 01:28 AM

Can we set this up so only certain users can see postbit addon?
PHP Code:

if (is_member_of($vbulletin->userinfo567))
{
    if (
$post['fieldx'])
    {
        
$template_hook['postbit_userinfo_right_after_posts'] .= '<dt>My FieldX</dt> <dd>' .$post[fieldx]. '</dd>';
    }


I can't seem to get that part to work. Thanks.

Lynne 01-20-2011 03:36 AM

It could be you need to globalize $vbulletin if you are using that variable.

Sarcoth 01-20-2011 03:59 AM

Quote:

Originally Posted by Sarcoth (Post 2151782)
Can we set this up so only certain users can see postbit addon?
PHP Code:

if (is_member_of($vbulletin->userinfo567))
{
    if (
$post['fieldx'])
    {
        
$template_hook['postbit_userinfo_right_after_posts'] .= '<dt>My FieldX</dt> <dd>' .$post[fieldx]. '</dd>';
    }


I can't seem to get that part to work. Thanks.

Quote:

Originally Posted by Lynne (Post 2151812)
It could be you need to globalize $vbulletin if you are using that variable.

Thanks Lynne. I know I had tried that earlier and thought it did not work. Going over my notes I see I used "$global $vbulletin;". I needed to leave out the first $ to fix the problem.

I thought globalizing it could be a security risk in some cases. Is there a problem doing that in this plugin?

Thank you again for the help on that. Now I can go to sleep. :)

Special Pages 03-13-2011 08:55 PM

Very good Lynne. Keep up the good work!

mikem164 03-18-2011 04:28 PM

Just did this and it turned out phenomenal! A big THANKS!

ExciterNL 05-08-2011 08:19 AM

Thanks

Booyakan 05-30-2011 04:47 PM

Thanks for this!

Also, I have one question...Let's say I use this in my template..

Code:

<vb:if condition="$post['field5']"><dt>My Field5</dt> <dd>{vb:raw post.field5}</dd></vb:if>

Is there anyway to still show it to guests? It disappears on me when I log out. Same holds true for using the plugin method.

Lynne 05-30-2011 05:07 PM

If the field is private (this has to do with your options when setting up the user profile field), then it won't show.

Booyakan 05-30-2011 05:52 PM

Quote:

Originally Posted by Lynne (Post 2201767)
If the field is private (this has to do with your options when setting up the user profile field), then it won't show.

Thanks Lynne, that's what it was.

Killeglass 05-31-2011 03:58 PM

Hello

I am trying to add this directly under the username

Code:

<vb:if condition="$post['fieldx']"><dt>My FieldX</dt> <dd>{vb:raw post.fieldx}</dd></vb:if>
when I do I get it formatted as


My Field
fieldx


where I would like


My Field fieldx


Any suggestions greatly appreciated


:)

Lynne 05-31-2011 04:20 PM

Can we get a link to see this?

Killeglass 05-31-2011 04:31 PM

Quote:

Originally Posted by Lynne (Post 2202125)
Can we get a link to see this?

Thank you Lynne!!


http://www.scikotics.com/forums


Tim :)

EDIT

here is how I have it installed

Code:

<div class="username_container">
                        <vb:if condition="$post['userid']">
                                {vb:raw memberaction_dropdown}
                                {vb:raw post.onlinestatus}
                        <vb:else />
                                <span class="username guest">{vb:raw post.musername}</span>
                        </vb:if>
                        </div>
                        <span class="usertitle">
                                {vb:raw post.usertitle}
                        </span>
                        <vb:if condition="$post['field6']"><dt>Name</dt> <dd>{vb:raw post.field6}</dd></vb:if>
                        <vb:if condition="$post['field5']"><dt>Ride</dt> <dd>{vb:raw post.field5}</dd></vb:if>
                        <vb:if condition="$post['rank']">
                                <span class="rank">{vb:raw post.rank}</span>


Lynne 05-31-2011 04:40 PM

You need to put it under the userinfo_extra div. Or, put it in it's own div with that class in order to get it layed out correctly.

Killeglass 05-31-2011 04:52 PM

Thank you again Lynne.

Unfortunately I am a bit old and senile LOL

What would the div class be? just guessing here.

Code:

<div>
<vb:if condition="$post['field6']"><dt>Name</dt> <dd>{vb:raw post.field6}</dd></vb:if>
                        <vb:if condition="$post['field5']"><dt>Ride</dt> <dd>{vb:raw post.field5}</dd></vb:if></div>

Would really like to keep this field up top if possible


Tim :)


EDIT


<dl class="userinfo_extra">

I added another instance and used that to achieve whats needed. I hope no advewrse reactions from using it

Thanks again!!!

Tim :)

8thos 06-23-2011 10:51 PM

Cool post.


All times are GMT. The time now is 02:16 PM.

Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2024, vBulletin Solutions Inc.

X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.01640 seconds
  • Memory Usage 1,879KB
  • Queries Executed 10 (?)
More Information
Template Usage:
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (19)bbcode_code_printable
  • (3)bbcode_php_printable
  • (8)bbcode_quote_printable
  • (1)footer
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (6)option
  • (1)pagenav
  • (1)pagenav_curpage
  • (2)pagenav_pagelink
  • (1)post_thanks_navbar_search
  • (1)printthread
  • (40)printthreadbit
  • (1)spacer_close
  • (1)spacer_open 

Phrase Groups Available:
  • global
  • postbit
  • showthread
Included Files:
  • ./printthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/class_bbcode_alt.php
  • ./includes/class_bbcode.php
  • ./includes/functions_bigthree.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • printthread_start
  • pagenav_page
  • pagenav_complete
  • bbcode_fetch_tags
  • bbcode_create
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • printthread_post
  • printthread_complete