Files
2024_WEBSITE_fipf/plugins/cipf_plugin/php/author_restriction.php
2024-03-11 00:02:55 +01:00

53 lines
1.1 KiB
PHP

<?php
/*
* it means someone outside wp is accessing the file, in this case kill it.
*/
if (!defined('ABSPATH')) {
die('You can not access this file!');
}
function restrict_author_page_CIPF() {
PLGNTLS_class::debug_infos();
$role_fipf = PLGNTLS_class::ROLE_FIPF;
$role_admin = PLGNTLS_class::ROLE_ADMIN;
if (!is_author())
return;
$current_user = wp_get_current_user();
/*
* check multiple user roles
* https://developer.wordpress.org/reference/functions/current_user_can/#div-comment-4083
* if user->role is found in array of allowed role, no redirection needed
*
*/
$allowed_roles = array($role_admin, $role_fipf);
if (array_intersect($allowed_roles, $current_user->roles))
return;
/*
* get_queried_object_id() would work too
* here get_the_author_meta works and is more explicit
*
$author_id = get_queried_object_id();
*/
$author_id = get_the_author_meta( 'ID' );
$current_user_id = get_current_user_id();
if ($current_user_id != $author_id) {
wp_redirect(home_url(), 301);
exit;
}
}
add_action('template_redirect', 'restrict_author_page_CIPF', 10);
?>