Last active
September 5, 2024 12:14
-
-
Save Qubadi/581bf1f0a68876e0781a6aaff3d43c48 to your computer and use it in GitHub Desktop.
Wordpress user, custom code ( Snippet PHP )display status account and Reactive/Deactive user name.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
add_filter('manage_users_columns', 'add_custom_user_column'); | |
function add_custom_user_column($columns) { | |
$columns['status'] = 'Status'; | |
return $columns; | |
} | |
add_action('manage_users_custom_column', 'show_custom_user_column_content', 10, 3); | |
function show_custom_user_column_content($value, $column_name, $user_id) { | |
if ('status' == $column_name) { | |
$user = get_userdata($user_id); | |
if ($user && in_array('administrator', (array) $user->roles)) { | |
return 'Active'; // Always show Active for administrators | |
} | |
$is_active = get_user_meta($user_id, 'is_active', true); | |
$link = $is_active ? add_query_arg(['action' => 'deactivate_user', 'user' => $user_id]) : add_query_arg(['action' => 'reactivate_user', 'user' => $user_id]); | |
$text = $is_active ? 'Deactivate' : 'Reactivate'; | |
return $is_active ? 'Active | <a href="' . wp_nonce_url($link, 'change_user_status') . '">' . $text . '</a>' : 'Inactive | <a href="' . wp_nonce_url($link, 'change_user_status') . '">' . $text . '</a>'; | |
} | |
return $value; | |
} | |
add_action('user_register', 'set_new_user_inactive'); | |
function set_new_user_inactive($user_id) { | |
$user = get_userdata($user_id); | |
if ($user && !in_array('administrator', (array) $user->roles)) { | |
update_user_meta($user_id, 'is_active', false); | |
} | |
} | |
add_action('admin_init', 'handle_user_status_change'); | |
function handle_user_status_change() { | |
if (current_user_can('administrator') && isset($_GET['action']) && isset($_GET['user']) && wp_verify_nonce($_GET['_wpnonce'], 'change_user_status')) { | |
$user_id = intval($_GET['user']); | |
$user = get_userdata($user_id); | |
if ($user && !in_array('administrator', (array) $user->roles)) { | |
if ($_GET['action'] == 'deactivate_user') { | |
update_user_meta($user_id, 'is_active', false); | |
} elseif ($_GET['action'] == 'reactivate_user') { | |
update_user_meta($user_id, 'is_active', true); | |
} | |
} | |
} | |
} | |
add_action('init', 'auto_logout_inactive_users'); | |
function auto_logout_inactive_users() { | |
if (is_user_logged_in()) { | |
$current_user = wp_get_current_user(); | |
if (!is_wp_error($current_user) && in_array('administrator', (array) $current_user->roles)) { | |
return; | |
} | |
$is_active = get_user_meta($current_user->ID, 'is_active', true); | |
if (!$is_active) { | |
wp_logout(); | |
wp_redirect(wp_login_url()); | |
exit; | |
} | |
} | |
} | |
add_filter('wp_authenticate_user', 'check_if_user_is_active', 10, 2); | |
function check_if_user_is_active($user, $password) { | |
if (is_a($user, 'WP_User') && !in_array('administrator', (array) $user->roles)) { | |
$is_active = get_user_meta($user->ID, 'is_active', true); | |
if (!$is_active) { | |
return new WP_Error('deactivated_account', __('Your account is deactivated.')); | |
} | |
} elseif (is_wp_error($user)) { | |
return $user; | |
} | |
return $user; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment