what is wrong with this code?
I really just want to display the current users role, but it seems impossible :-(
I have tried this:
<?php if ( is_user_logged_in() ) {
global $current_user; get_currentuserinfo();
} else { }
global $current_user;
get_currentuserinfo();
echo 'User role: ' . $current_user->role . "";
?>
And this:
<?php
$current_user = wp_get_current_user();
printf( __( 'Netværksgruppe: %s', 'textdomain' ), esc_html(
$current_user->role ) ) ;
?>
and about 100 more, but nothing seems to work...
(I have tried with plugins, php snippets, functions.php, anything)
Any help would be much appreciated :-)
The user role isn't a string value you can output like that. Take a look at the WP_User object.
$user->roles is actually an array. Most WP installations result in each user having a single role. but there are many reasons why a user may have multiple roles. So you'll either need to output all of the roles using something like implode() or just output the first role by index.
// Current WP_User object
$user = wp_get_current_user();
// All Roles (implode the array)
printf( 'All my roles are: %s.', implode( ', ', $user->roles ) );
# Result: "All my roles are: administrator, custom_role, some_role."
// Or single role (only output the first result)
printf( 'My role is: %s.', $user->roles[0] );
# Result: "My role is: administrator."
See documentation of get_currentuserinfo() -
https://codex.wordpress.org/Function_Reference/get_currentuserinfo
$current_user - is object, not string. If you need email of curent user:
global $current_user;
get_currentuserinfo();
echo 'User email: ' . $current_user->user_email . "\n";
Related
I am trying to change certain elements for a certain user, in the back administration area of WordPress.
I have found the following code which works for a specific role, but how do I change it to target a specific user?
function wpa66834_role_admin_body_class( $classes ) {
global $current_user;
foreach( $current_user->roles as $role )
$classes .= ' role-' . $role;
return trim( $classes );
}
add_filter( 'admin_body_class', 'wpa66834_role_admin_body_class' );
To clarify: instead of identifying role in the foreach section and then adding the role to the class name, I would like it to identify the user by their specific name or ID, and then add that name or ID to the class name.
You should be able to use get_current_user_id() to (as the name of the function suggests) get the current user's id, and then perform a simple if statement to compare it with a specific ID number.
Example:
function wpa66834_role_admin_body_class( $classes ) {
$user_id = get_current_user_id();
// Assuming 13 is the user's ID you're targeting
if( $user_id == 13 )
$classes .= ' custom-class-for-user-13';
return trim( $classes );
}
add_filter( 'admin_body_class', 'wpa66834_role_admin_body_class' );
More info on how to use the function can be found here.
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
So, this one has my head in for a loop. I've contacted the Support of the plugin and they basically told me I'm SOL, but I refuse to believe there isn't a way to do this.
I'm using AffiliateWP to attribute sales to a single location. Each Affiliate feeds off of a Wordpress User, and for their nickname I've used their address/location.
Unfortunately, the plugin doesn't include this in the Order Details screen or Order Emails, which is a BIG problem. I've frankensteined some code together but keep getting all kinds of undefined errors because they do everything inside classes, which I guess makes sure that people like me can't fiddle in there?
Function for assigning an affiliate to an order: https://github.com/AffiliateWP/AffiliateWP/blob/master/includes/integrations/class-woocommerce.php#L78
Function for retrieving the Affiliates User ID: https://github.com/AffiliateWP/AffiliateWP/blob/master/includes/affiliate-functions.php#L204
Function for retrieving the Affiliates Name: https://github.com/AffiliateWP/AffiliateWP/blob/master/includes/affiliate-functions.php#L132
[Edit] And finally, my Frankenstein:
add_action( 'woocommerce_admin_order_data_after_billing_address', 'my_custom_checkout_field_display_admin_order_meta', 10, 1 );
function my_custom_checkout_field_display_admin_order_meta($order){
global $Affiliate_WP_WooCommerce;
echo '<p><strong>'.__('My Field').':</strong> ' . affiliate_wp()->affiliates->get_affiliate_name( $affiliate_id ) . '</p>';
}
This doesn't throw me an error, but it does throw me a blank area...
Try this:
add_action( 'woocommerce_admin_order_data_after_billing_address', 'my_custom_checkout_field_display_admin_order_meta', 10, 1 );
function my_custom_checkout_field_display_admin_order_meta($order){
global $Affiliate_WP_WooCommerce;
$order_id = $order->get_id();
$referrals = affiliate_wp()->referrals->get_by( 'reference', $order_id);
$affiliate_id = $referrals->affiliate_id;
echo '<p><strong>'.__('My Field').':</strong> ' . affiliate_wp()->affiliates->get_affiliate_name( $affiliate_id ) . '</p>';
}
the affwp_get_affiliate_name method returns blank on three conditions:
if ( ! $affiliate = affwp_get_affiliate( $affiliate ) ) {
return '';
}
if ( ! $user_info = get_userdata( $affiliate->user_id ) ) {
return '';
}
...
// If neither are set, return an empty string.
if ( empty( $first_name ) && empty( $last_name ) ) {
return '';
}
Start by manually passing an affiliate id you know to be associated with a record that contains a $first_name and/or $last_name, for example:
echo '<p><strong>'.__('My Field').':</strong> ' . affiliate_wp()->affiliates->get_affiliate_name(5) . '</p>';
This will rule out if the problem is with the $affiliate_id that you're currently passing it. If you get values returned, it should be a simple matter to fix.
After trying that I would experiment with changing the priority of the add_action(), raising it to 999 or something because it could possibly be executing before the relevant hooks in the plugin.
I am looking to display content based on multiple different user roles.
The aim is to display content for two or three different user roles, and then block it for other user roles, displaying a message that it is only for certain logged in users.
So far, I have the following:
<?php
global $user_login, $current_user;
get_currentuserinfo();
$user_info = get_userdata($current_user->ID);
$roles = array (
'administrator',
'subscriber',
);
if (is_user_logged_in() && in_array( $roles, $user_info->roles)) {
//content here
} else {
// they aren't logged in, so show them the login form
}
?>
At the moment, the issue I am having seems to be that the code is looking for both the administrator and subscriber roles at the same time and as a result, the if statement is not satisfied and the login form shows.
If I change $roles to 'administrator' or 'subscriber', it then works fine.
So, how would I search through the array to display either role, not all of them.
Thanks
You could use:
array_intersect (link)
array_intersect will check between two array to see if the needle ($roles) exists in the haystack ($user_info->roles). I have tested this against my own and works well.
See below the use of array_intersect.
<?php
global $user_login, $current_user;
get_currentuserinfo();
$user_info = get_userdata($current_user->ID);
$roles = array (
'administrator',
'subscriber',
);
if (is_user_logged_in() && array_intersect( $roles, $user_info->roles)) {
echo 'success';
} else {
echo 'failure';
}
?>
Example 1: LINK the $roles array does not match.
Example 2: LINK the $roles array has one match.
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';
?>