How get the language in wordpress, which current user set in his profile?
Something like:
$language = get_current_user()->language;
if ($language == 'en') {
//gogogo;
}
If you want the user meta locale (which is set in user profile), you can try this:
$user = wp_get_current_user();
$user_locale = get_user_meta($user->ID, 'locale', true);
if($user_locale == 'en_EN') {
// gogogo;
}
Greetz, Bjorn
Related
I have a ticket support form on my site which right now has a field which returns (in the admin area) the name of the person who submitted the form.
Anyone know how I would modify this to display their user role instead? ie. Subscriber, Editor, etc.
$raised_by='';
if($ticket->type=='user'){
$user=get_userdata( $ticket->created_by );
$raised_by=$user->display_name;
}
I'm guessing it'll be something with this stuff in it...but I'm not too savy when it comes to this.
function get_user_role() {
global $current_user;
$user_roles = $current_user->roles;
$user_role = array_shift($user_roles);
return $user_role;
}
Please change last line of your code to this:
$raised_by=ucwords($user->roles[0]);
So that your current code which display First Name i.e.
$raised_by='';
if($ticket->type=='user'){
$user=get_userdata( $ticket->created_by );
$raised_by=$user->display_name;
}
Above code will become:
$raised_by='';
if($ticket->type=='user'){
$user=get_userdata( $ticket->created_by );
$raised_by=ucwords($user->roles[0]);
}
Update: To remove underscore with space your code may become as:
$raised_by='';
if($ticket->type=='user'){
$user=get_userdata( $ticket->created_by );
$raised_by= ucwords(str_replace("_"," ",$user->roles[0]));
}
You may notice, I have added ucwords function of PHP also, it is to make sure , roles on the screen look good, i.e. admin will be shown as Admin etc.
Also you may notice roles[0], 0 means that data currently we have there is as an array. So we are picking the first user roles from all the roles assigned to the user. I am sure it will be sufficient for your needs.
Let me know if this solves your issue or you still need any help. You can post in comments. Or Update your question.
You could use this line of code.
$raised_by='';
if($ticket->type == 'user'){
$user = get_userdata( $ticket->created_by );
$raised_by = implode(', ', $user_info->roles);
}
Or, if you prefer to use the get_user_role function that you've written,
slightly modify it to take the user ID as input and return the user role.
function get_user_role($user_id) {
$user_info = get_userdata($user_id);
$user_roles = $user->roles;
$user_role = array_shift($user_roles);
return $user_role;
}
You could use it like as shown below to output the user role.
$raised_by='';
if($ticket->type == 'user'){
$user = get_userdata( $ticket->created_by );
$raised_by = get_user_role($user->ID);
}
I have a woprpress site and need the user automatically login in another server no wordpress when he will visite that site. Is that possible?
You can set cookie or session or etc...
With this data you can run auto login function.
My example:
function auto_login() {
// this works perfectly
$user_login = 'admin';
// this does not work even when setting the same variable via query string ?user=admin
// $user_login = $_GET['user'];
//get user's ID
$user = get_userdatabylogin($user_login);
$user_id = $user->ID;
//login
wp_set_current_user($user_id, $user_login);
wp_set_auth_cookie($user_id);
do_action('wp_login', $user_login);
}
add_action('init', 'auto_login');
Iam working with buddypress,
I have a two user roles,
1-student
2-faculty
and i have set default user role as subscriber.
when user registers and activates account by clicking on link sent through mail.User role changes to default(subscriber).
Any idea what is the issue? Below is the code assigning role to user on sign up.
add_action('bp_core_signup_user', 'ad_user_signup_usermeta', 10, 5);
function ad_user_signup_usermeta($user_id, $user_login, $user_password, $user_email, $usermeta) {
if(isset($_POST['signup_membership']) && !empty($_POST['signup_membership']))
update_user_meta($user_id, 'membership', $_POST['signup_membership']);
$userdata = array();
$userdata['ID'] = $user_id;
if(!empty($_POST['signup_usertype'])) {
if($_POST['signup_usertype'] == 'student') {
$userdata['role'] = 'student';
}
if($_POST['signup_usertype'] == 'instructor') {
$userdata['role'] = 'instructor';
}
}
if ($userdata['role']){
wp_update_user($userdata);
}
}
Upon activation, BuddyPress (at least version 2.0.2) updates the user's role to the default role.
https://buddypress.trac.wordpress.org/browser/tags/2.0.2/bp-members/bp-members-functions.php#L1560
You can comment out that line, or write some code to work around it. I'm using "WP Roles At Registration" and ran across the same problem. I ended up adding a filter on bp_core_signup_user to save the original role but you'll want to add something like this to your ad_user_signup_usermeta:
update_user_meta($user_id, 'temp_role', $role_name)
then reset it back in a filter for bp_core_activated_user
public function after_bp_activated_user($user_id, $key, $user) {
$user = get_userdata($user_id);
$role = get_user_meta($user_id, 'temp_role');
if ($role) {
$user->set_role($role[0]);
}
}
add_filter('bp_core_activated_user', array($this, 'after_bp_activated_user'), 30, 3);
I have added custom fields on registration to allow the user to input their own password and have also created code to generate a verification code which then gets emailed over to the user. Of course, the users needs to click the link in the email before they can log in.
Here's where I am stuck. I am trying to add my own authentication to check the status of the verification when the user tried to log in.
Here's my code which isn't working;
function check_validation_status($username) {
$user = get_user_by('login', $username);
$userID = $user->ID;
$status = get_user_meta($userID, 'verified', true);
if($status == '0') {
return;
}
}
add_action('wp_authenticate', 'check_validation_status');
Unfortunately this code doesn't seem to do anything. I have also tried the following (hooking into a different action)
function check_validation_status($username) {
$user = get_user_by('login', $username);
$userID = $user->ID;
$status = get_user_meta($userID, 'verified', true);
if($status == '0') {
wp_logout(); // works but doesn't show an error :(
}
}
add_action('wp_login', 'check_validation_status');
This code is successfulling logging the user straight out if they are not verified however it shows no form of error to the user, they just get redirected straight back to the login page.
Logging the user in but straight back out seems like a sloppy way to do it, is there a way to prevent the log in in the first place?
I have managed to fix this issue now. I instead needed to hook into wp_authenticate_user and return a WP_Error. Here is my working code, I hope it helps someone out in the future.
function check_validation_status($user, $password) {
$userID = $user->ID;
$status = get_user_meta($userID, 'verified', true);
if($status == '0') {
$errors = new WP_Error();
$errors->add('title_error', __('<strong>ERROR</strong>: This account has not been verified.', 'podium'));
return $errors;
}
return $user;
}
add_action('wp_authenticate_user', 'check_validation_status', 10, 2);
I'm looking for a way to exclude the admin user or user 1 from user search results in drupal 7.
I want this to not show up for security reasons.
If you build a custom search using Views, you can set the filter(s) to only show users with certain roles. Do not expose the filter to users. If User 1 has the "administrator" role and everyone else has another (non-administrator) role, when you apply the filter, only non-administrator accounts will show up when a search is run.
You can implement hook_preprocess_search_result() by inserting the following into template.php. Note you'll have to clear the Drupal cache to enable this function in Configuration -> Performance -> Clear Cache
function <themename>_preprocess_search_result(&$variables) {
global $language;
$display_result = true;
if( $variables['module'] == 'user' ) {
if( $variables['user']->uid == 1 || $variables['id'] == 1 ) {
$display_result = false;
$variables = array();
}
}
if( $display_result ) {
$result = $variables['result'];
$variables['url'] = check_url($result['link']);
$variables['title'] = check_plain($result['title']);
if (isset($result['language']) && $result['language'] != $language->language && $result['language'] != LANGUAGE_NONE) {
$variables['title_attributes_array']['xml:lang'] = $result['language'];
$variables['content_attributes_array']['xml:lang'] = $result['language'];
}
$info = array();
if (!empty($result['module'])) {
$info['module'] = check_plain($result['module']);
}
if (!empty($result['user'])) {
$info['user'] = $result['user'];
}
if (!empty($result['date'])) {
$info['date'] = format_date($result['date'], 'short');
}
if (isset($result['extra']) && is_array($result['extra'])) {
$info = array_merge($info, $result['extra']);
}
// Check for existence. User search does not include snippets.
$variables['snippet'] = isset($result['snippet']) ? $result['snippet'] : '';
// Provide separated and grouped meta information..
$variables['info_split'] = $info;
$variables['info'] = implode(' - ', $info);
$variables['theme_hook_suggestions'][] = 'search_result__' . $variables['module'];
}
}
Another way to do it would be search-result.tpl.php and check the $info_split array to check if the search result was user one and not display any output.
Don't forget that you'll need to also prevent users from visiting site.com/user/1 as they'll be able to get just as much information from this page.