I've created an ACF Checkbox field that I assigned to the User Profile area.
I'm using a form in order to bring up a popup with the checkboxes where users choose from the available options, the form then saves and updates the choices in user's profile. That all seems to be working perfectly fine.
But now I need to show the selected choices in User Profile template using a shortcode.
My ACF return value is set to value and I'm using the snippet exactly as per ACF's docs in order to display a list of labels.
This is my shortcode:
function get_user_profile_choices() {
$field = get_field_object('color_choices');
$colors = $field['value'];
// Display labels.
if( $colors ): ?>
<ul>
<?php foreach( $colors as $color ): ?>
<li><?php echo $field['choices'][ $color ]; ?></li>
<?php endforeach; ?>
</ul>
<?php endif;
}
add_shortcode( 'user-profile', 'get_user_profile_choices' );
I am currently just testing this with my own admin account, so I'm trying to show my choices in my profile.
I'm not getting anything. Could it be because the checkbox is in User Profile? Do I need to declare any user variable? Any ideas?
Check this out. This way you will get the value for the currently logged-in user.
function get_user_profile_choices() {
$user_id = get_current_user_id();
$field = get_field_object('color_choices', 'user_'.$user_id);
}
add_shortcode( 'user-profile', 'get_user_profile_choices' );
Add second parameter "user_USERID" in the function "get_field_object" in order to get value from the user profile fields.
change "1" to your user ID
$user_id = 1;
$field = get_field_object('color_choices', 'user_' . $user_id);
Related
I have returned a list of registered users. I am looking to be able to query these results and also generate a click through page. Is that possible.
Im trying to essentially get a list of users by searching (first name for example), then click through to show information about them, on the front end. Its mainly the click through im having difficulty with.
For example if i could click a member and it would go through to a page that had the user first name, last name etc.
<?php
$blogusers = get_users();
// Array of WP_User objects.
foreach ( $blogusers as $user ) {
if ( in_array( 'team_member', (array) $user->roles ) ) {
?>
<div class="member_user">
<a href="<?php echo the_permalink();?>">
<?php
echo $user->first_name." ".$user->last_name;
?>
</a>
</div>
<?php
}
}
?>
If i understood correctly, you already have a list of users, and you want each one of them to be linkable to its own page, where you would display the info you want, such as first name, last name etc.
Firstly you have to query for your users and loop through them to display them in a list. You already have this in your code, but if you want something more advanced a query would look like this:
$authors = get_users(array(
'role__not_in' => array('Subscriber', 'Administrator'),
//if you do not want to list the admins and subscribers
'orderby' => array(
'post_count' => 'DESC',
//order users by post count (how many posts they have)
)
));
foreach ($authors as &$author){
//loop through the users
$user_info = get_userdata($author->ID);
$user_link = get_author_posts_url($author->ID);
?>
<a href="<?php echo $user_link; ?>" style="display:block;">
<?php echo $user_info->first_name.' '.$user_info->last_name; ?>
</a>
<?php
}
Then you need to create the page for the user. WordPress handles this by author.php page. If your theme does not have one, simple create a php file called author.php and place it at the root folder of your theme.
You may edit author.php to include the data you want. However if you remove the list of posts that are displayed by default for the selected user you will loose this functionality in your theme.
So inside author.php you may get the user like so:
//get current user (object):
$current_author = (isset($_GET['author_name'])) ? get_user_by('slug', $author_name) : get_userdata(intval($author));
//get User data by user ID
$user_info = get_userdata($current_author->ID);
//echo first & last name:
echo $user_info->first_name.' '.$user_info->last_name;
If you make a print_r($user_info) you may see what info you can display for the selected user.
You may also use the 'get_the_author_meta' function to get more info from the user's profile, eg
get_the_author_meta('description', $current_author->ID);
You can find more info about this function here: https://developer.wordpress.org/reference/functions/get_the_author_meta/
I would generate a link like this:
<?php
echo $user->first_name." ".$user->last_name;
echo 'view user';
?>
And then query the user ID on the next page to get the desired information.
On the next page you can:
<?php
$user_ID = $_GET['user_id'];
get_header();
?>
and then query this to get pretty much any content related to that user
Two and a half approaches:
You could customize your theme's author archive template to display the desired information. Three author archives are available in the template hierarchy:
author-{nicename}.php
author-{id}.php
author.php
You'd be able to link to https://{site_url}/author/{username} for each user.
However, this may not be ideal if you want to keep that normal author blog archive and add the new profile pages. So what may be better long term is to setup a custom post type for the member profiles, then every time a new "member" user is created run a function that creates a new post representing the user.
You can register that custom post type manually with register_post_type() or if you need help a good plugin is Custom Post Type UI.
A good action for your function to hook into is user_register since it happens immediately after user registration but will have access to user data. Within your function you can create the new post using wp_insert_post().
You can then display the information of that custom post type using the archive-$posttype.php and single-$posttype.php templates.
2 1/2. You may also consider using only the custom post type and not querying for users at all. If this member profile is the only reason some folks are users it may even be more convenient this way. Then you can use the archive-$posttype.php and single-$posttype.php templates easily to display the information you choose.
There is a function for that. Use get_author_posts_url()
In the required template use the following code:
// only return required user based on role
$args = array(
'role' => 'team_member'
);
$blogusers = get_users( $args );
// loop though and create output
foreach ( $blogusers as $user ) : ?>
<div class="member_user">
<a href="<?php echo get_author_posts_url( $user->ID );?>"> // links to author page
<?php echo $user->first_name." ".$user->last_name; ?>
</a>
</div>
<?php endforeach;
You will then need to customise the author.php template in your theme to render as required.
I would like to be able to use the same form on several pages, and know which page a submitted form came from. For a long list of reasons, I need to do that based on category.
I found code to add categories to pages, works great.
But I can't figure out how to get Gravity Forms to dynamically populate a field with the category.
I've selected "Allow field to be populated dynamically" on the form, I've set the parameter name to "pagecategory"
Here's what I've got - it does nothing:
//Get Page Category - For Demo Form
add_filter("gform_field_value_pagecategory", "populate_pagecategory");
function populate_pagecategory($value){
global $post;
$value = get_the_category( $post->ID,'metakeyname',true);
return $value;
}
get_the_category() actually returns an array of all category that is assigned to that post. The code below works for me to fetch the name of first category only.
//Get Page Category - For Demo Form
add_filter("gform_field_value_pagecategory", "populate_pagecategory");
function populate_pagecategory($value){
global $post;
$categories = get_the_category( $post->ID,'metakeyname',true);
$value = $categories[0]->cat_name;
return $value;
}
I'm a bit surprised this type of need/question isn't more prevalent. I had to do this same thing, but for a custom taxonomy. In case anyone lands here in need of how to do Mash's answer, but with a custom taxonomy, the following is working for me. My custom taxonomy is "services," which you can see replaces 'metakeyname' in Mash's answer.
//Get Page Custom Taxonomy - For Gravity Forms
add_filter("gform_field_value_pagecategory", "populate_pagecategory");
function populate_pagecategory($value){
global $post;
$terms = get_the_terms( $post->ID,'services',true);
$value = $terms[0]->name;
return $value;
}
I would like to show a custom welcome message for each of my registered shop customers. Something like "Welcome CUSTOMERNAME! You have been with us for one year now. To say thanks, we are giving you a discount on everything in our shop of 10%."
I have created a custom field welcome_message with the plugin Advanced Custom Fields and would like to show the value of it in the account dashboard of my customers at frontend.
At the backend, I am showing the textarea at the user profile page and I am adding the field with this code:
$message = get_field( 'welcome_message' );
echo esc_attr( $message );
This code is placed here: /wp-content/themes/flatsome-child/woocommerce/myaccount/dashboard.php
But somehow the value is not coming up. I have also tried it with simply the_field('welcome_message'); or to add [acf field="{$welcome_message}"] at the account page in WordPress but that didn't work either. Somehow the value is not coming through.
I hope someone could help me out a litte.
Thanks in advance.
Best regards
It's necessary that get_field function is connected to user.
Eg:
<?php
$variable = get_field('field_name', 'user_1');
?>
In case of repeater field:
<?php if( have_rows('repeater', 'user_1') ): ?>
<ul>
<?php while( have_rows('repeater', 'user_1') ): the_row(); ?>
<li><?php the_sub_field('title'); ?></li>
<?php endwhile; ?>
</ul>
<?php endif; ?>
I currently have an advanced custom field setup in the user's profile with a field name of author_title. This would display what the user does at the company blog.
I currently have the following working but the title only updates for first user and all users get that title instead of being able to use their own.
Updated
<?php $current_user = wp_get_current_user(); ?>
<h3><?php the_field('author_title', 'user_' . $current_user->ID); ?></h3>
Try to get user ID in another way.
Now you get ID for currently logged in user so it shows the same field everywhere. I assume you need show that filed for author posts so:
$author_id = get_the_author_meta('ID');
$author_field = get_field('author_title', 'user_'. $author_id );
On my site when a user is registering there is an option to pick from 3 values (House, car, boat). Once the user has registered and viewing their profile, I want to show a button on the front-end based on what meta value they have selected when registering. If they have no meta key, then nothing is shown.
Here is my code attempt, but does not work!
<?php global $current_user;
get_currentuserinfo(); //wordpress global variable to fetch logged in user info
$userID = $current_user->ID; //logged in user's ID
$havemeta1 = get_user_meta($userID,'house',true); //stores the value of logged in user's meta data for 'house'
$havemeta2 = get_user_meta($userID,'car',true); //stores the value of logged in user's meta data for 'car'
$havemeta3 = get_user_meta($userID,'boat',true); //stores the value of logged in user's meta data for 'boat'
?>
<!--add if statement to figure out what button to show to logged in user-->
<?php if ($havemeta1) { ?>
<div class="Button1"></div>
<?php } elseif ($havemeta2) { ?>
<div class="Button2"></div>
<?php } elseif ($havemeta3) { ?>
<div class="Button3"></div>
<?php } else { ?>
<div></div>
<?php }?>
In short - The expected result is, if the user has choose House when registering, then show the DIV with the class=Button1 etc etc
Update:
This code above now works!