I have a line that prints the contact information of a user in his profile. Only logged in user can see the contact information.
<?php
// Contact Info
?>
I have 2 user roles - supervisor, manager
What I am trying to achieve is that if the logged in user is a supervisor then he can see his own contact info, but he cannot see manager's contact.
But If the logged in user is a manager then he can see his own contact info, but he can also see supervisor's contact info.
I have been trying to use the standard if current_user_is function, but this will only display one or the other.
<?php global $current_user;
get_currentuserinfo();
if(current_user_is("manager"))
echo 'Contact Info Here';
else if(current_user_is("supervisor"))
echo 'Contact Info Here';
else if(current_user_is_not("manager"))
echo 'Restricted contact, must be a manager';
?>
I just can't figure out how to make this work, so it displays conditionally.
I also have these rules in the global rule, not sure how relevant it is :
$store_user = get_userdata( get_query_var( 'author' ) );
$store_info = get_store_info( $store_user->ID );
$user_meta = get_user_meta($store_user->ID);
From the question, it looks like you'll need to call the get_users function and pass the "supervisor" role to get users of that type and display their contact info to the "manager" role.
As you've written it, the contact info being displayed is for the current user. You'll need to grab another role's contact info if that's what you want to display.
Untested but this may be close...
//if current user is the manager...
$supervisors = get_users( 'role=supervisor' );
foreach ($supervisors as $supervisor) {
echo '<span>' . esc_html( $supervisor->user_email ) . '</span>';
}
I'm nothing more than a beginner with php but using 'OR' may help you aggregate roles' conditions on a single piece of content:
<?php global $current_user;
get_currentuserinfo();
if(current_user_is("manager") OR current_user_is("supervisor"))
echo 'Contact Info Here';
else()
echo 'Restricted contact, must be a manager';
?>
Related
I am trying to make a link for user to navigate to, when they are displayed what group they belong to:
<?php $user = wp_get_current_user();?>
<?php printf( '%s.', $user->roles[0] );?>
Very nicely the code echos what role (group) the user belongs to, now i just need it to be a link to the corresponding page (same name as the user role)
e.g "You are in group Alpha" (where Alpha needs to be a link to the page Alpha)
Can anyone help with this?
href="#" is what you need to change.
for example:
<?php $user = wp_get_current_user();?>
<?php printf( '%s.', $user->roles[0] );?>
or if you need to use printf:
<?php $user = wp_get_current_user();?>
<?php printf( '%s.', $user->roles[0] );?>
or even:
<?php $user = wp_get_current_user();?>
<?php printf( '%s.', $user->roles[0] );?>
but echo is totally enough in your case.
Please note, that this is a basic knowledge about HTML, and before programming in PHP you should consider learning that first.
How a post in wordpress add comment only for logged users?
visitors must answer their own add in comments. but i need a post, leave a comment (only for logged users) and users must register to site beforce add comment to post
can use functions file to do it ?
i found that 2 code but not work and do not know how to use it:
in facing link find this:
https://codex.wordpress.org/Function_Reference/wp_get_current_user
Code:
<?php
$current_user = wp_get_current_user();
if ( 0 == $current_user->ID ) {
// Not logged in.
} else {
// Logged in.
}
?>
and in facing link find this:
https://codex.wordpress.org/Function_Reference/comment_form
code:
'<p class="must-log-in">' . sprintf( __( 'You must be logged in to post a comment.' ), wp_login_url( apply_filters( 'the_permalink', get_permalink( ) ) ) ) . '</p>'
and from the combination of these two together, I built below function!
<?php
$current_user = wp_get_current_user();
if ( 0 == $current_user->ID ) {
// Not logged in.
'<p class="must-log-in">' . sprintf( __( 'You must be logged in to post a comment.' ), wp_login_url( apply_filters( 'the_permalink', get_permalink( ) ) ) ) . '</p>'
} else {
// Logged in.
}
?>
and add this to my post with Insert PHP Code Snippet plugin for use only (a) post
please help me to do this. thank you all
Settings > Discussion you will find the option: Users must be registered and logged in to comment. - http://prntscr.com/k2f02d
Checked this option and save the settings your requirement will be fulfilled.
Review below menu navigation
Settings >>> Discussion
Checked the "Users must be registered and logged in to comment " checkbox
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 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 );
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.