Get "Name" profile field from buddypress in user_register hook - wordpress

I'm making a wordpress plugin that needs to grab the user's name on registration when integrated with BuddyPress. Unfortunately, the Name field never returns anything. The following code runs in the user_register hook.
add_action( 'user_register', 'ts_api_registration_save', 9999999, 1 );
function ts_api_registration_save($user_id)
{
$info = get_userdata($user_id);
$name = bp_profile_field_data( array( 'field' => 'Name', 'user_id' => $info->ID));
}
Unfortunately $name is always empty. I have checked the database, and the Name field is the only one in bp_xprofile_fields, so I know it exists.
I have also confirmed that $info->ID is not empty, and it correctly contains a user id. Any idea why this isn't working?
In the mean time I have been able to do this to get what I want:
$results = $GLOBALS['wpdb']->get_results( 'SELECT * FROM ' . $wpdb->prefix . "bp_xprofile_fields WHERE name = 'Name'" );
$fullName = $_POST["field_" . $nameFieldId];
But this is not optimal.

This function only works in the context of a user profile page: bp_profile_field_data
Instead, try using xprofile_get_field_data
Also - the 'user_register' hook probably runs too early.
Try using one of these hooks:
bp_core_signup_user
bp_signup_pre_validate
bp_signup_usermeta

Try to use xprofile_get_field_data() instead of bp_profile_field_data()
For example:
$name = xprofile_get_field_data( 'Name', $user_id, $multi_format = 'comma' );
echo $name;
http://hookr.io/functions/xprofile_get_field_data/
Another solution
Try to pass the field id rather than the field name.
$value = xprofile_get_field_data($field_id, $user_id);
You can get the field id by looking at the url in wp-admin when you edit that field, or just rolling over the edit button.

Related

Update ACF select field data programmatically

I have a ACF select field with a field name of "sales_rep". It is populated with:
John Doe
Bart Simpson
Eric Cartman
The field is set to show on the "User Form" and only show when the "Current User Role" is an admin.
The field shows on each user page and I can manually change it to the sales rep I want and save it.
The issue is that I want to be able to programmatically update it whenever the user profile is saved. I will eventually add more code to determine the sales rep but I simplified the issue for this post.
Below is my code in functions.php:
Attempt 1: No error and no change to value
// Update sales rep
add_action( 'edit_user_profile_update', 'update_rep' );
add_action( 'personal_options_update', 'update_rep' );
function update_rep( $user_id )
{
update_user_meta( $user_id, 'sales_rep', 'Eric Cartman' );
}
Attempt 2: No error and no change to value
// Update sales rep
add_action( 'edit_user_profile_update', 'update_rep' );
add_action( 'personal_options_update', 'update_rep' );
function update_rep( $user_id )
{
$field_key = "sales_rep";
$value = array("Eric Cartman");
update_field( $field_key, $value, $user_id );
}
Seems like user_ prefix could fix the issue.
Try to get/update fields with user_ prefix.
F.e:
update_field( $field_key, $value, "user_$user_id" );
And for update_user_meta,
Calling delete_user_meta before could solve the problem.
User meta could have duplicate keys.
Try this one
delete_user_meta( $user_id, $field_key );
add_user_meta( $user_id, $field_key, $value );
I think #Andrii Kovalenko is right, user_ prefix could be your issue.
Also noticed you are saving acf field data as an array("Eric Cartman").
I am assuming your ACF select field predefined select options look like this...
John Doe
Bart Simpson
Eric Cartman
This should work for your acf option...
// is run when you edit YOUR profile, and save it
add_action('personal_options_update', 'handle_user_profile_update' );
// is run when you edit ANY OTHER profile and save it
add_action('edit_user_profile_update', 'handle_user_profile_update' );
// on user profile update function handler
function handle_user_profile_update($user_id) {
// update sales rep
update_sales_rep_field($user_id);
// add anymore on user update profile functions here...
}
// update sales rep field
function update_sales_rep_field($user_id) {
// our values
$value = 'Eric Cartman';
// update acf user field by user id
update_field('sales_rep', $value, 'user_' . $user_id);
}
Out of curiosity, are these sales reps actually users in your wordpress site?
If so you can dynamically populate the select sales_rep user field with sales rep users on your site like this...
function render_sales_rep_select_field($field) {
// reset sales_rep select field choices
$field['choices'] = [];
// get users with user role sales
$args = array(
'role' => 'sales', // your sales reps user role
'orderby' => 'user_nicename',
'order' => 'ASC'
);
// get user with args from above
$users = get_users( $args );
// for each sales rep user
foreach ( $users as $user ) {
// build our sales rep select field choices by user_id > full name
$field['choices'][$user->id] = $user->first_name . ' ' . $user->last_name
}
// return the select field
return $field;
}
// load user field with our custom select drop of sales rep users
add_filter('acf/load_field/name=sales_rep', 'render_sales_rep_select_field');
With this method you won't need to update the field when the user saves changes to their profile.
For example if you get_field like this...
// get user field sales rep array
get_field('sales_rep', 'user_' . $user_id ); // random user id
it will return an array of user_id and full name...
Array
(
[5] => Eric Cartman
)
In your acf user select field settings you will need select return both value and label.

Unable to save Query Vars with a post

I'm trying to save Query Vars to a post for later retrieval.
I'm using permalinks in this format: domain.com/%category%/%postname%/
Example:
I create a following page
domain.com/page-003/
I add Query Var called email to the page
add_query_arg('email', 'test#abc.com', 'domain.com/page-003/')
Now when I call
get_permalink($post_id);
I get
domain.com/page-003/
Instead of
domain.com/page-003/?email=test#abc.com
What am I missing? Aren't Query Vars saved with a post?
You want to save some meta data which you want to restore later on and add as query arg into the URL.
You need to first save it as post_meta like e.g. when you save post with that data. You use:
<?php update_post_meta(get_the_ID(), 'email_address', 'abc#mail.com'); ?>
More details: https://codex.wordpress.org/Function_Reference/update_post_meta
Then during the retrieval, you may hook into a HOOK early on like template_redirect or earlier of the post you can get post_meta to get the email and then add to query arg:
<?php $email = get_post_meta( get_the_ID(), 'email_address' ); ?>
Then
esc_url( add_query_arg( 'email', $email, get_permalink( get_the_ID() ) ) );
Something like that, code is untested, I just wrote here, you may please read detailed doc in codex for each function used above.
Update: How to update/fill Ninja form field from Meta Value:
add_filter( 'ninja_forms_render_default_value', 'wm_the_value' , 10 , 3);
function wm_the_value( $default_value, $field_type, $field_settings ) {
if( 'textbox' == $field_type && in_array('ref' , $field_settings)){
$default_value = get_post_meta(get_the_ID(),'_listing_mls', true);
}
return $default_value;
}
'ref' is field name in Ninja form.
'_listing_mls' is meta_key name from WP database.
Hope it works for you.

Add Wordpress User meta information in admin

I'm looking to add a custom field into my users section of the wordpress admin, I have a registration field that has an input for company, i can see the company value in the wp_usermeta table but need to get it in the users column in the admin, any ideas on how to achieve this?
You will need to add action and filter hooks for showing custom value in users table. Place the following into your theme's functions.php. Notice: 'PLACE COLUMN NAME HERE' and 'PLACE META KEY HERE':
function add_custom_column_name($columns) {
$columns['columns_array_name'] = 'PLACE COLUMN NAME HERE:';
return $columns;
}
function show_custom_column_values($value, $column_name, $user_id) {
if ( 'columns_array_name' == $column_name )
return get_user_meta( $user_id, 'PLACE META KEY HERE', true );
return $value;
}
add_filter('manage_users_columns', 'add_custom_column_name');
add_action('manage_users_custom_column', 'show_custom_column_values', 10, 3);

Create links for custom field query

All my post has a custom field created_by_alias, I am basically use the value in place of whereever author is used. I have customized the entry-meta function to display this value in plain text, I have also learned to build a query:
$query = new WP_Query( array('meta_key' => 'created_by_alias',
'meta_value' => 'somevalue' ));
Now for the final part: how to create a link for this piece of text, that brings a listing of all posts by this alias?
Do I need to write a template file or is there a quick way to reuse whatever code that generates links to a category, tag listing?
Not sure what you are looking for but seems to me that you want to show a link for the author posts in a specific page, if so, then you can use (using your query)
$query = new WP_Query( array('meta_key'=>'created_by_alias', 'meta_value'=>'somevalue' ));
If $query returns result and you have the id then you can use
echo get_author_posts_url( $query->posts[0]->ID ); // the `ID` from first row
Or for multiple rows, you can loop like
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
echo get_author_posts_url( get_the_ID() ) . '<br />';
}
}
Once this link is clicked, then posts will be retrieved and will be shown according to your page hierarchy, if no author template was found then finally the inedx.php will be used.
Also, if you need a custom template and want to query
$posts = get_posts(array($ID);
Now, to get the id you an use
get_user_by( $field, $value ); // 'id', 'slug', 'email', or 'login'
So, if you have the login name of the user, for example admin then you can use
$id = get_user_by( 'login', 'admin' )->ID; // php 5+
// or
$user = get_user_by( 'login', 'admin' );
$id = $user->ID;
Then get the link and echo it, whereever you want
echo get_author_posts_url( $id );

Extra Profile Field Not Showing In Wordpress User Profile

I have created a an extra profile field on Buddypress registration page for users to select their native language using the profile fields area on Buddypress.
Once the user has registered and made this selection I want it their input to show on user-edit.php in wordpress backend so the admin of the site can see what native language that user is.
My extra profile field id is 136
How would I get this to show on the user-edit.php page?
Put this function in your theme/functions.php or in plugins/bp-custom.php
It should appear at the bottom of user-edit.php
function show_136_field ( $user ) {
$field_value = xprofile_get_field_data( 136, $user->ID, $multi_format = 'comma' );
echo "<br />Language: " . $field_value . "<br />";
}
add_action( 'edit_user_profile', 'show_136_field' );
You can also get the field value with the field name rather than id.
$field_value = xprofile_get_field_data( 'Language', $user->ID, $multi_format = 'comma' );
If the $field_value is an array and $multi_format = 'comma', a csv string will be returned.

Resources