We have a complicated weird system where we need to check if the wordpress administrator has logged in and by logged in i dont just mean the session but initial second when the query is sent to validate the credentials entered. I was thinking doing traditional php where I do something like the following:
if(isset($_POST['submit']) { // do something }
But that would be the incorrect way. Where exactly would i look to see this peice , i believe its wp-login.php but wasnt able to find what i was looking for.
thanks
try
global $current_user;
$user = get_userdata($current_user->ID);
$userRole = (!empty($user->roles) ? $user->roles : '');
if(in_array('administrator', $userRole)) {
// do your stuff
}
or you can try :- http://codex.wordpress.org/Function_Reference/current_user_can
You can use very neat Wordpress function to determine the user capabilities, it is called current_user_can():
<?php
if ( current_user_can('update_core') ) {
echo 'The current user is Administrator or Super administrator (in Multisite)';
}
Only Administrators of single site installations have the below list of capabilities. In Multi-site, only the Super Admin has these abilities:
update_core
update_plugins
update_themes
install_plugins
install_themes
delete_themes
edit_plugins
edit_themes
edit_users
create_users
delete_users
unfiltered_html
See here for more information.
We can add some custom action inside the filter hook login_redirect. If the form was not submitted, the $user param is a WP_Error Object.
add_filter( 'login_redirect', function( $redirect_to, $requested_redirect_to, $user )
{
if( !is_wp_error( $user ) && in_array( 'administrator', $user->roles ) )
{
# Do your thing; here just inspecting the WP_User Object
wp_die(
sprintf( '<pre>%s</pre>', print_r( $user, true ) ),
'Var dump',
array(
'response' => 500,
'back_link' => true
)
);
}
return $redirect_to;
}, 10, 3 );
Related
This is a query based around WooCommerce Membership and Subscriptions.
I must add that I'm also trying to decide if it's good UX to do what I'm doing.
There are many solutions to redirecting users after they log in but I have a situation where I want to redirect a user with the role of 'subscriber' when they click on specific links to pages that describe and allow you to become a member. So although I don't want to hide 'join now' etc I just want those to redirect to the my-account page.
Again there are various roles and redirect plugins but none seem to help in this specific scenario. So the source of the code I've used is here: SOURCE and I'm looking to do something like this:
function eks_redirect_users_by_role() {
global $post;
$current_user = wp_get_current_user();
$role_name = $current_user->roles[0];
if ( 'subscriber' === $role_name && $post->ID == 47145) {
wp_redirect( '/my-account' );
}
}
add_action( 'admin_init', 'eks_redirect_users_by_role' );
So if the user role is a subscriber and they try and visit the page idea it's redirected.
At the current time, it does fall back to a product page which says 'you already have a membership' but it's multiple steps to arrive.
This might be more useful and proper way to achieve your request.
function redirection_based_user_role(){
$current_user = wp_get_current_user();
$role_name = $current_user->roles[0];
$postid = get_the_ID();
if ( 'subscriber' === $role_name && $postid == 47145 ) {
wp_redirect( home_url( '/my-account' ), 301 );
exit;
}
}
add_action( 'template_redirect', 'redirection_based_user_role' );
Hope this works.
I was able to achieve want I wanted to the following way:
function eks_redirect_user() {
$current_user = wp_get_current_user();
$role_name = $current_user->roles[0];
$postid = get_the_ID();
if ( 'subscriber' === $role_name && $postid == 47145 ) { ?>
<script>
function redirectPage() {
window.location = "/my-account/";
}
redirectPage();
</script>
<?php
exit;
}
}
add_action('wp_footer', 'eks_redirect_user' );
The issue is it's a fairly untidy solution as the redirect feels odd. I'm not sure if using wp_redirect would work better. I decided to do was just disable the button on the page with the main membership information rather than redirecting every call to action to the account page which seems like a more elegant solution.
A lot of these guides are 'redirect after login' I have mulitple user types with different levels of access, I'd like to write some code which if a 'Subscriber' role goes onto the page (Which they do not have access it'll redirect them.
Here's one of the many code snippets I've tried, but can't get it to work! Help :)
function consultant_page_redirect() {
$current_user = wp_get_current_user();
$role_name = $current_user->roles[0];
if (('subscriber' === $role_name ) &&( is_single( array(2864, 2630)) ) ) {
wp_redirect('/trial-user-page/');
}
}
function consultant_page_redirect( $user_login, $user ) {
$role_name = $user->roles[0];
if (('subscriber' === $role_name ) &&( is_single( array(2864, 2630)) ) ) {
wp_redirect('/trial-user-page/');
}
}
add_action('wp_login', 'consultant_page_redirect', 10, 2);
Add this snippet code on child theme functions.php
I'd like to change the role of all users with a specific domain name (#example.com) to a custom role when my plugin is activated.
I found some code for doing it when a user registers and attempted to adapt it to my needs but it doesn't seem to work. Nothing happens when I activate the plugin. The roles don't change and I don't get any errors popping up so I'm not quite sure what I'm doing wrong.
I'm still learning PHP so please forgive me if this doesn't make sense.
Here's my code:
function set_role_by_email( $user_id ){
$user = get_user_by( 'id', $user_id );
$domain = substr(
strrchr(
$user->data->user_email,
"#"
), 1
); //Get Domain
$custom_role_domains = array( 'example.com' );
if( in_array( $domain, $custom_role_domains ) ){
foreach( $user->roles as $role )
$user->remove_role( $role ); //Remove existing Roles
$user->add_role( 'custom_role' ); //Add role to user
}
}
register_activation_hook( __FILE__, 'set_role_by_email' );
You could try this code and see if it works:
function set_role_by_email()
{
$users = get_users();
foreach ($users as $user) {
if (strpos($user->user_email, '#example.com')) {
foreach ($user->roles as $role) {
$user->remove_role($role);
}
$user->add_role('custom_role');
}
}
}
register_activation_hook(__FILE__, 'set_role_by_email');
Make sure this code is placed in the main plugin file.
Is it possible to create a function via functions.php that restricts users from creating pages based on their user role? So for example, users with the user role "limited" can only create 5 pages.
When they reach this amount, the "Create new page" button must be gone or they have te receive a message that they reached there limit and have to contact there admin.
I know there is a plugin called Bainternet Posts Creation Limits but this is causing some problems.
Is something possible with a function?
EDIT:
Ok, I tried the code below but aint working and to be honest my coding skills are to poor to make something of it.
Anybody else any ideas?
You could try something like this:
Step 1: Upon user creation: give your user the capability 'publish_pages'
Step 2: Save in the user_meta how many pages the user has created.
Step 3: If the user has 5 or more pages remove the capability from this user.
STEP 1 & 2 (untested):
add_action( 'user_register', 'user_registered', 10, 1 );
function user_registered( $user_id ) {
// ADD CAPABILITY
$user = new WP_User( $user_id );
if ( ! empty( $user->roles ) && is_array( $user->roles ) && in_array( 'YOURROLEHERE', $user->roles ) ) {
$user->add_cap( 'publish_pages');
update_user_meta($user_id, 'numberofpages', 0);
}
}
STEP 3 (untested):
add_action('publish_page', 'user_published_page');
function user_published_page(){
// GET CURRENT USER
$user_id = get_current_user_id();
$user = new WP_User( $user_id );
// CHECK ROLE
if ( ! empty( $user->roles ) && is_array( $user->roles ) && in_array( 'YOURROLEHERE', $user->roles ) ) {
// GET NUMBER OF PAGES CREATED
$numberofpages = intval(get_user_meta($user_id, 'numberofpages', true));
if($numberofpages >= 5){
$user->remove_cap( 'publish_pages');
} else {
update_user_meta($user_id, 'numberofpages', $numberofpages + 1);
}
}
}
Hello friends,
i need little help in Wordpress. I am trying to hide the authorbox that appears under the post for specific user only.
Example if post i am looking into is posted by admin, then i want to hide the authorbox under post content that is posted by admin, but should show for all other users ? i tried different functions but not able to success on this.
I want to completly hide the authorbox, i know it can be done with css code that is
.author-box { display:none;}
but this hides the overall authorbox of complete them, i just want to hide the authorbox on the posts that are made by admin ?
i am using genesis framework, so please suggest if any help you can make here.
Thanks
In your theme's functions.php file, add something like this:
function remove_my_post_metabox() {
global $post;
// If the post author ID is 1, then remove the meta box
// You would need to find the ID of the author, then put it in place of the 1
if ( $post->post_author == 1) {
remove_meta_box( 'authordiv','post','normal' ); // Author Metabox
}
}
add_action( 'add_meta_boxes', 'remove_my_post_metabox' );
If you need to find out the role of the author, and truly do it based on the role (admin, for example), then it would be more like this:
function remove_my_post_metabox() {
global $post;
// If there's no author for the post, get out!
if ( ! $post->post_author) {
return;
}
// Load the user
$user = new WP_User( $post->post_author );
// Set "admin" flag
$is_admin = FALSE;
// Check the user roles
if ( !empty( $user->roles ) && is_array( $user->roles ) ) {
foreach ( $user->roles as $role ) {
if ( $role == 'administrator' ) {
$is_admin = TRUE;
}
}
}
// Lastly, if they ARE an admin, remove the metabox
if ( $is_admin ) {
remove_meta_box( 'authordiv','post','normal' ); // Author Metabox
}
}
add_action( 'add_meta_boxes', 'remove_my_post_metabox' );