Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/234.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 只有管理员允许访问wp管理员和登录?_Php_Wordpress - Fatal编程技术网

Php 只有管理员允许访问wp管理员和登录?

Php 只有管理员允许访问wp管理员和登录?,php,wordpress,Php,Wordpress,我想这样做,只有管理员(角色)允许与wp管理员登录,而不是任何其他角色,其他用途,如(编辑,作者)是从前端登录,它的工作正常,但我想只有管理员可以通过wp管理员登录 我使用了ultimate会员插件进行前端登录。 此外,我使用了下面的代码访问wp管理员只为管理员(角色),但它不工作 <?php function restrict_admin(){ //if not administrator, kill WordPress execution and provide a message

我想这样做,只有管理员(角色)允许与wp管理员登录,而不是任何其他角色,其他用途,如(编辑,作者)是从前端登录,它的工作正常,但我想只有管理员可以通过wp管理员登录

我使用了ultimate会员插件进行前端登录。

此外,我使用了下面的代码访问wp管理员只为管理员(角色),但它不工作

<?php
function restrict_admin(){
//if not administrator, kill WordPress execution and provide a message
   if( !current_user_can('administrator') ) {
        wp_die( __('You are not allowed to access this part of the site') );
    }
}
add_action( 'admin_init', 'restrict_admin', 1 );
?>

试试这个

add_action( 'admin_init', 'redirect_non_admin_users' );
/**
 * Redirect non-admin users to home page
 *
 * This function is attached to the 'admin_init' action hook.
 */
function redirect_non_admin_users() {
    if ( is_admin() !== false) {
        wp_redirect( home_url() );
        exit;
    }
}
我测试了这个,它成功了,我认为它简单易行
你可以在这里找到

谢谢你们的帮助,我在回答我自己的问题,希望这也能帮助其他人

我已经提到了这个链接

我已经用hook解决了这个问题

 add_action( 'wp_authenticate' , 'check_custom_authentication' );
  function check_custom_authentication ( $username ) {

    $username;
     $user = new WP_User($username);
     $user_role_member=$user->roles[0];



    if($user_role_member == 'author' || $user_role_member == 'editor'){
        session_destroy();
        wp_redirect( home_url() );
        exit;
    }

 }

如果有人需要相同的代码,这里只允许管理员、作者和编辑使用/wp-login.php登录

//------------- This website is read only - so only staff can log in  -------------
add_filter( 'authenticate', 'myplugin_auth_signon', 30, 3 );
function myplugin_auth_signon( $user, $username, $password ) {
    $user_role_member=$user->roles[0];
    if(!in_array($user_role_member,array('administrator','author','editor'))){
         wp_logout();
         return new WP_Error( 'broke', __( "This website is read only for regular users", "your_wp_domain_name" ) );
        exit;
    }else{
        return $user;   
    }
}
//------------- this website is read only - so staff can log in  ------------------

您可以在数组上方的数组中添加\删除角色(“管理员”、“作者”、“编辑器”)

我也尝试过这种方法,但不起作用。编辑器,作者用户仍在登录。@hermnath它仍不工作,作者和编辑用户仍在登录。@sameersheikh您确定用户角色是编辑器吗?好的,请尝试
if(1){
而不是
if(is_admin()!==false){
让我们测试它的工作,或者我们是否通过了true(1)在if语句中,这意味着每次访问wp admin时都将执行此操作。其他一些操作出错。您可以直接使用。安装后,转到“设置”>“仪表板访问”,然后选择“仅限管理员”。这些操作正常,但我只需要管理员(角色)可以通过wp-admin而不是编辑器登录,作者和订户用户可以通过wp-admin登录。如果选择正确的选项(“仅限管理员”)就像我告诉过你的,只有管理员才能访问wp admin…我使用了那个插件,但仍然存在相同的问题。编辑和作者用户仍然登录。很难理解你在说什么-如果你使用编辑帐户登录,你将被重定向到哪个页面?它正在工作。这不检查角色,只检查具有“激活插件”功能。此外,您还缺少“激活插件”的引号。这不会锁定已登录的用户,对吗?此外,如果用户是多个角色的成员,这也不起作用。您必须测试所有用户角色,而不仅仅是第一项。
//------------- This website is read only - so only staff can log in  -------------
add_filter( 'authenticate', 'myplugin_auth_signon', 30, 3 );
function myplugin_auth_signon( $user, $username, $password ) {
    $user_role_member=$user->roles[0];
    if(!in_array($user_role_member,array('administrator','author','editor'))){
         wp_logout();
         return new WP_Error( 'broke', __( "This website is read only for regular users", "your_wp_domain_name" ) );
        exit;
    }else{
        return $user;   
    }
}
//------------- this website is read only - so staff can log in  ------------------