How to display a custom field?
-
Hello!
I’m quite new with Buddypress. So I’m searching for hours already how I can do the following thing. I hope you guys can help me!
I made some custom fields beyond ‘Profile Fields’. I would like to display the outcome of this fields on another page (on a order, within LearnPress). I tried so many PHP codes with ID’s, names etc. But none of them works… What am I doing wrong?
Would love to hear from you. Thanks in advance 🙂
Lot
-
Hi,
Not used LearnPress so unable to comment much on that . The method I used to insert/display my own custom profile fields into a header on the user activity page was as follows (in this case, a Location field which was created in the WP dashboard).
1. Add to bp-custom.php :
//Add Location field to header if user has entered data into it //Nothing is shown if user hasn't entered anything into field function bptest_show_location_field () { $location_field = bp_get_member_profile_data( 'field=Location' ); if ( ! $location_field ) { return; } else { echo '<span class="custom-field-text">' . $location_field . '</span>'; } } //user_bio_location_field is used within the member-header php template file add_action ( 'user_location_field', 'bptest_show_location_field' );
2. In my member-header.php file (which was copied into my child theme) entered the div :
<div id="user-location"> <?php do_action ( 'user_location_field' ); ?> </div><!-- #user-location - function is found in bp-custom -->
Obviously the php code in step two is placed wherever you want the field to display, just put it in the correct location of the correct php file. In my case it was member-header.php but for you i’m guessing its the one that generates your order page?.
Not sure if you have already tried any of that or if it even helps!
Hi mcUK!
Thanks for you reply. I’m working with Lotte on this issue.
I tried adding your code. Everything should be in place right now, but it’s still not showing the custom fields. I’m probably using the code wrong…
I tested your code with a custom field ‘Opleiding’ which I added in WordPress: Users -> Profile fields:
I added this to bp-custom.php:
<?php function bptest_show_opleiding_field () { $opleiding_field = bp_get_member_profile_data( 'field=Opleiding' ); if ( ! $opleiding_field ) { return; } else { echo '<span class="custom-field-text">' . $opleiding_field . '</span>'; } } add_action ( 'user_opleiding_field', 'bptest_show_opleiding_field' ); ?>
Then I added this to order.php (the php file where the custom field should pop-up):
<div id="user-opleiding"> <?php do_action ( 'user_opleiding_field' ); ?> </div>
What am I doing wrong? It’s probably a typo or a wrongly added term… But I can’t figure out what it is!
Hope you can help us out! Thanks for the help so far!
David
You don’t explain the context of
order.php
.bp_get_member_profile_data
is meant for use in the context of members loop, etc.You can use it outside that context, but you need to pass in the user id.
$args = array( 'field' => 'Opleiding', 'user_id' => bp_loggedin_user_id() ); $opleiding_field = bp_get_profile_field_data($args);
You can also use this approach:
xprofile_get_field_data( $field, $user_id = 0, $multi_format = 'array' )
For example:$user_id = bp_loggedin_user_id(); $opleiding_field = xprofile_get_field_data( 'Opleiding', bp_loggedin_user_id(), $multi_format = 'comma' );
Yes, it can be confusing.
You should always find & review the function you are trying to use within the codebase.
Doing so will reveal what the function expects to receive.Thanks for the quick reply!
Ohhhh, I think I know why it’s not working. It’s because order.php is in de Learnpress plugin, and trying to get data from Buddypress. I’m not using your code inside Buddypress only…
So: order.php in the Learnpress plugin needs to pull the data from the Buddypress plugin. When I change
Opleiding: <?php do_action ( 'user_opleiding_field' ); ?>
in order.php to your new suggestion it doesn’t work either. Should your new suggestion work with a secondairy plugin (Learnpress?).
Sorry if I’m asking dumb questions. I’m do have some PHP knowledge, but not enough to figure this kind of stuff out myself. I hope you can help us out!
David
Did you also try
xprofile_get_field_data
?You can always make a direct sql call to the database using the $wpdb global.
https://codex.wordpress.org/Class_Reference/wpdbHi David,
Can’t see any errors in your code, so it should work for BuddyPress (try the do_action within a BP file to double check). Hopefully Shane’s suggestions will work for you!
Hi Shanebp!
Yeah, I tried the xprofile_get_field_data suggestion, but this also doesn’t work for me…
I’ve got this in bp-custom.php:
<?php //Add Location field to header if user has entered data into it //Nothing is shown if user hasn't entered anything into field function bptest_show_opleiding_field () { $opleiding_field = bp_get_member_profile_data( 'field=Opleiding' ); if ( ! $opleiding_field ) { return; } else { echo '<span class="custom-field-text">' . $opleiding_field . '</span>'; } } //user_bio_location_field is used within the member-header php template file add_action ( 'user_opleiding_field', 'bptest_show_opleiding_field' ); ?>
and this in order.php in the Learnpress plugin:
<div id="user-opleiding"> Opleiding: <?php $user_id = bp_loggedin_user_id(); $opleiding_field = xprofile_get_field_data( 'Opleiding', bp_loggedin_user_id(), $multi_format = 'comma' ); ?> </div>
I looked at the url you have send, but I am not able to figure out how to use this, unfortunately. Maybe you have another suggestion for me to try?
Sorry if I’m bordering you guys too much! I hope you are able to figure this one out with me 🙂 It would mean a lot!
David
I’M MAKING PROGRESS!!
mcUK suggestion to test the code in Buddypress was a very good tip. This is what I found out:
<div id="user-opleiding"> <?php do_action ( 'user_opleiding_field' ); ?> </div>
WORKS IN BUDDYPRESS 🙂 I added this
do_action
in the standard profile page, and it is showing the fieldOpleiding
.Every other suggestion, like xprofile_get_field_data, doesn’t work.
NOW: do one of you know how I can let
do_action
work when I call it from another plugin? That would be awesome!I showed you how to use
bp_get_profile_field_data
– but you didn’t make the changes.Your function in bp-custom won’t be called if order.php does not contain a
do_action
.Hi Shane,
I’m sorry… I think I just don’t understand how you are implementing the code. Can you tell me where I should copy which code from your suggestion so I can call the custom field across plugins and outside the context of the members loop?
For your info: order.php looks like this right now, with the
do_action
added.<h2 class="user-display-name"> <?php $current_user = wp_get_current_user(); echo 'Username: ' . $user->user_login . '<br />'; echo 'User email: ' . $user->user_email . '<br />'; echo 'Name: ' . $user->display_name . '<br />'; echo 'Opleiding: ' . do_action ( 'user_opleiding_field' ); //echo 'User ID: ' . $user->ID . '<br />'; // ?> </h2>
Hi David,
I just tried this (note ive used the ‘Location’ field as that is what i have as a field):
function bptest_show_opleiding_field () { $user_id = bp_loggedin_user_id(); $opleiding_field = xprofile_get_field_data( 'Location', bp_loggedin_user_id(), $multi_format = 'comma' ); if ( ! $opleiding_field ) { return; } else { echo $opleiding_field ; } } add_action ( 'user_opleiding_field', 'bptest_show_opleiding_field' );
Tested with this in the cover-image-header.php
<div id="fieldId"> <?php do_action ( 'user_opleiding_field' ); ?> </div>
This shows up fine when used on my site. Try this with the do_action in your order.php? Hope it works
THIS WORKED 🙂 I’ll try and implement this with more field and get back to you when I run into problems, but for now this is working great! Thank you so much for your time and effort!
Got there in the end thanks to Shane 🙂
And thanks to you as well mcUK!
Thanks for all the help! So far it’s working great 😉
Got one question now that I’m working with the provided code:
Do you have tips for using this code for multiple custom fields? How should I edit this code in bp-custom.php if I want to add multiple custom fields?
<?php function bptest_show_education_field () { $user_id = bp_loggedin_user_id(); $education_field = xprofile_get_field_data( 'Highest Education Grade', bp_loggedin_user_id(), $multi_format = 'comma' ); if ( ! $education_field ) { return; } else { echo 'Hoogste opleiding: ' . $education_field . '<br />'; } } add_action ( 'user_education_field', 'bptest_show_education_field' ); ?>
I’m now trying to copy this code for every custom field, but that’s not really a clean way of doing this. And it’s not working either. Hope you can help me out with this one as well!
Hi David,
Copying the code method does work. (Renaming the function, the variables/fields within and the add_action bits).
Just tried this out too and it appears to work fine, though not tested thoroughly so you’d need to double check:
function bptest_show_a_field () { $user_id = bp_loggedin_user_id(); $field_one = xprofile_get_field_data( 'Field One', bp_loggedin_user_id(), $multi_format = 'comma' ); $field_two = xprofile_get_field_data( 'Field Two', bp_loggedin_user_id(), $multi_format = 'comma' ); if ( ! $field_one && ! $field_two ) { return; } else { echo $field_one ; echo $field_two ; } } add_action ( 'user_a_field', 'bptest_show_a_field' );
Then put
do_action ( 'user_a_field' );
in your desired location.Oh and you can use some <span> (see earlier posts) to get control style wise
Ah, thanks! That works great!
Now I’m running in a more serious issue… I discovered that all the code is working, but I’m using it to display data filled in by users in the backend of WordPress (order.php). BUT the code is only showing the filled in data from the user that is logged, while I need to be able to see the data filled in by every individual user that filled in the form.
I hope you understand what I’m aiming for!
David
EDIT: I think it has something to do with this line:
$user_id = bp_loggedin_user_id();
I’ve found
bp_displayed_user_id()
, but the code won’t display any values when I replace everybp_loggedin_user_id()
withbp_displayed_user_id()
Hi!
Is anyone able to help me out with this one?
David
Hello @stilld
I am sorry, i didn’t read everything carefully. From your last comment what i can understand is
bp_displayed_user_id() – This function only work on any user profile.
bp_loggedin_user_id() – Logged in user id is global function can work anywhere.I hope you get this.
Thanks.
Hi Jigesh!
Thanks for the info!
What I’m trying to achieve is that I need to be able to view the fields that are filled in by any random user who placed a order.
At this moment the code doesn’t show the data filled in by the user I select, but the data that is filled in by me (the one who is logged in).
I need to be able to open a order and view the data a user filled in the custom register form and not the data that I filled in (the current logged in user).
I hope you understand my question!
Hello @stilld
I feel i can surely help you in this, can you please print your order screenshot here ?
I mean your order page admin screenshot and not codes.
Thanks.
Jigesh,
Thanks for the help! This is how it looks right now:
As you can see to the right of the profile image their are some data showing from the custom fields. @mcUK and @shanebp helped me out achieving this. BUT, this is not the correct data it should be showing for this user… It shows data that the logged in user filled in.
I logged in as the user that filled in the correct data, and then it’s showing the right data for the user. But this is because I’m logged in as that user…
NOTE: I’m transferring this data across two plugins: buddypress and learnpress. Just so you know!
This is the php in bp-custom:
<?php function bptest_show_a_field () { $user_id = bp_loggedin_user_id(); $firstname = xprofile_get_field_data( 'First Name', bp_loggedin_user_id(), $multi_format = 'comma' ); $lastname = xprofile_get_field_data( 'Last Name', bp_loggedin_user_id(), $multi_format = 'comma' ); $birth = xprofile_get_field_data( 'Date of birth', bp_loggedin_user_id(), $multi_format = 'comma' ); $education = xprofile_get_field_data( 'Highest Education Grade', bp_loggedin_user_id(), $multi_format = 'comma' ); $profession = xprofile_get_field_data( 'Profession', bp_loggedin_user_id(), $multi_format = 'comma' ); if ( ! $firstname && ! $lastname && ! $birth && ! $education && ! $profession ) { return; } else { echo 'Voornaam: ' . $firstname . '<br />' ; echo 'Achternaam: ' . $lastname . '<br />' ; echo 'Geboortedatum: ' . $birth . '<br />' ; echo 'Hoogste graad: ' . $education . '<br />' ; echo 'Beroep: ' . $profession . '<br />' ; } } add_action ( 'user_a_field', 'bptest_show_a_field' ); ?>
Hello @stilld
Is there any kind of data showing of actual user on order page?
If not, than i need to check database fields of leanpress order table , there must be user id passed in the database.
Thanks.
- The topic ‘How to display a custom field?’ is closed to new replies.