From 0ab26c0b3178f4e22a8997bd6ea61c4cdc9d826d Mon Sep 17 00:00:00 2001 From: asus Date: Wed, 20 Mar 2024 01:54:49 +0100 Subject: [PATCH] - hide admin bar for front user - admin page redirects non admin users --- plugins/cipf_plugin/cipf_plugin.php | 2 +- plugins/cipf_plugin/php/hide_admin.php | 57 ++++++++++++++++++++++++-- 2 files changed, 54 insertions(+), 5 deletions(-) diff --git a/plugins/cipf_plugin/cipf_plugin.php b/plugins/cipf_plugin/cipf_plugin.php index b5d4dac..998e328 100644 --- a/plugins/cipf_plugin/cipf_plugin.php +++ b/plugins/cipf_plugin/cipf_plugin.php @@ -4,7 +4,7 @@ Plugin Name: cipf_plugin Plugin URI: Description: Author: hugogogo -Version: 0.3.3 +Version: 0.3.9 Author URI: */ diff --git a/plugins/cipf_plugin/php/hide_admin.php b/plugins/cipf_plugin/php/hide_admin.php index eafb9b9..7b299e4 100644 --- a/plugins/cipf_plugin/php/hide_admin.php +++ b/plugins/cipf_plugin/php/hide_admin.php @@ -21,7 +21,7 @@ function hide_admin_bar_CIPF() { /* - * is admin page + * on admin page, don't hide bar of course * */ if (is_admin()) { @@ -30,14 +30,63 @@ function hide_admin_bar_CIPF() { /* - * if + * check multiple user roles + * https://developer.wordpress.org/reference/functions/current_user_can/#div-comment-4083 * */ - if (!current_user_can($role_admin)) { - show_admin_bar(false); + $current_user = wp_get_current_user(); + $allowed_roles = array($role_admin, $role_fipf); + if (array_intersect($allowed_roles, $current_user->roles)) { + return; } + show_admin_bar(false); } add_action('after_setup_theme', 'hide_admin_bar_CIPF'); + + + +/* +* prevent users to access admin page +* +*/ +function restrict_admin_access_CIPF() { + PLGNTLS_class::debug_infos(); + $role_admin = PLGNTLS_class::ROLE_ADMIN; + $role_fipf = PLGNTLS_class::ROLE_FIPF; + + /* + * this concerns logged_in users, for admin page + * + */ + if (!is_user_logged_in()) { + return; + } + if (!is_admin()) { + return; + } + + /* + * some roles are allowed to access the admin panel + * + */ + $current_user = wp_get_current_user(); + $allowed_roles = array($role_admin, $role_fipf); + if (array_intersect($allowed_roles, $current_user->roles)) + return; + + /* + * every other roles are redirected + * + */ + redirection_profil_CIPF(); +} +add_action('init', 'restrict_admin_access_CIPF', 100); + + + + + + ?>