Wordpress query users and display as taxonomy - php

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.

Related

Potential Security Issues with Passing Wordpress Variable as a Parameter on get_permalink()

I need to make sure I'm not opening up a security hole in my site. I have a Custom Post Type called "Communities" and a custom post type called "Floor Plans" and need to add a parameter to the URL on Single Floorplan posts.
My goal is that when a Floorplan post object is selected from a Communities Single post, it appends the name of the community (i.e. single community post title) to the URL for use on the following page. So, enabling query vars:
<?php
//* Enable queryvars (functions.php)
add_filter('query_vars', 'parameter_queryvars' );
function parameter_queryvars( $qvars )
{
$qvars[] = "community";
return $qvars;
}
I have a template part called "dynamic-floor-plans" that lives in a loop at the bottom of Single Communities posts. I'm grabbing the single_post_title of the current communities page, and passing that to the URL so that it's available using $_GET
The reason we opted for this method is that the "Floor Plans" post type has a canonical/default version that has the same properties as its Community-specific counterparts. In order to avoid a post relationship situation (which would give the user flat out wrong data in some cases) the community versions just have to be able to use
<?php
//* dynamic-floor-plans template part (placed in loop on communities page)
global $post;
$string = strtolower(single_post_title( null, false ));
$slug = str_replace(' ','-',$string);
?>
<div class="card-button">
<a class="small expanded secondary button"
href="<?php echo get_the_permalink() . '?community=' . $slug ?>">View Floorplan</a>
</div>
Now, this totally is working. I'm able to grab that community variable on the subsequently-selected floorplan single page using $_GET and things seem to be working nicely on the front end:
<?php
//* Single Floorplans Template
$community = $_GET["community"];
?>
<h1 class="entry-title"><?php echo $title ?></h1>
<h4>
<?php if ($community) { ?>
At <?php echo $community; } ?>
</h4>
...
I just can't help but feel that this isn't kosher, or that there's a more secure way to do this.
Am I open to exploitation? I can include the function for the loop itself as well, but figure this code will be sufficient to get started in evaluation.

avatar is not displaying in custom template

I have created a custom profile page in which I want to show profile picture. I use the below code to show the avatar:
<div class="activity-avatar">
<?php bp_activity_avatar(); ?>
</div>
<?php bp_displayed_user_avatar( 'type=full' ); ?>
<?php global $userdata; get_currentuserinfo();
echo get_avatar($userdata->ID, 46 ); ?>
But I don't get the avatar of user. If I use <?php bp_activity_avatar(); ?> outside of the main <div> then it shows the avatar of the last updated profile picture, not of the current user. But I don't get Avatar at the needed position. How can I get avatar at any position in template? I use bootstrap classes, could the problem occur from there?
Try:
$userid = bp_loggedin_user_id();
echo bp_core_fetch_avatar( array( 'item_id' => $userid) );
You don't explain the context of your custom profile page, so $userid might need to be changed to $userid = bp_displayed_user_id() or however you determine the user id for a page view.

How to get member list based on role by using buddypress?

I am new in buddypress.
My problem is: I have create a template for get member list based on role Like:
<?php if ( bp_has_members( bp_ajax_querystring( 'members' ). '&per_page=25&role=author' ) ) : ?>
<ul id="members-list" class="item-list row kleo-isotope masonry">
<?php while ( bp_members() ) : bp_the_member(); ?>
<li><?php bp_member_avatar(); ?></li>
<?php endwhile; ?>
</ul>
But i am not getting user list based on role. Please help me and suggest me any idea.
It's a bit more complicated than you think.
bp_has_members() doesn't support getting users by role. But it supports getting users by their IDs. So the solution might be like this:
Get array of IDs of users you need:
$blogusers = get_users( 'fields=ID&role=author' );
Instead of role=author add this string to bp_has_members() params:
include='.implode(',', $blogusers)
Thus you will get users of your role.
Don't forget, that you can add ordering to get_users() and bp_has_members() call - that will reflect the order of users displayed on a page.

How to put Wordpress Profile Meta in different pages?

I need three different pages showing different parts of one user profile on wordpress. Author.php is a standard feature and is great is loads on my website www.website.com/site1/user/nicename. However I want to have one profile which is sorted, but display the profile over three different areas of the website.
PLEASE NOTE: Username in this example would be joe-blogs1990 what ever the user decides, the nicename will be formatted joebloggs, first and surname combined for nice permalinks on wordpress.
www.website.com/site1/author/username
www.website.com/site2/agency/nicename
www.website.com/site3/meet-the-team/nicename
I simply wish to call user profile information in three different places, but they will have different information on each one, I’ve managed to create a place where everything is edited in one place, however to display information based on the URL is slightly bewildered me.
Here is what I have so far, but nothing is loading on any of the pages what im trying to achieve above.
<?php get_header(); ?>
<?php $blogname = get_bloginfo('name'); if ( $blogname == 'site1' ) {
get_the_author_meta('description');
} ?>
<?php $blogname = get_bloginfo('name'); if ( $blogname == 'site2' ) {
get_the_author_meta('first_name');
get_the_author_meta('last_name');
} ?>
<?php $blogname = get_bloginfo('name'); if ( $blogname == 'site3' ) {
get_the_author_meta('facebook_profile');
get_the_author_meta('twitter_profile');
} ?>
<?php get_footer(); ?>
Try adding echo in Front of the get_the_whatever methods. All methods prefixed with get_ just return strings; in order to see something you have to echo it.

wordpress blog index is post page

I have a custom page named 'Journal', which I use as a blog index page for my wordpress website. I've run into a rather strange problem. When I enter <?php echo get_the_title(); ?> or whatever in home.php, it returns the title of a post, instead of the page title 'Journal'. Is anyone familiar with this problem?
Thanks!
This is the expected behavior for this page. When you set a page to be your "blog", you can't access the template tags for that page. Instead, the template tags are for the loop of the posts to be displayed on that page.
To get the title, you have to first get the id of that page, and then you can pass it to a function:
<?php
$page_for_blog = get_option( 'page_for_posts' );
$page_title = get_the_title( $page_for_blog );
?>
Now you can print the $page_title and you should see "Journal".
Updated with Advanced Custom Fields
Now that you have the Journal page's id ($page_for_blog), you can get your field values with:
$field_value = get_field( 'field_name', $page_for_blog );
Obviously, replace 'field_name' with whatever field you're trying to retrieve.

Categories