如何在wp-admin重定向中包含非用户?

时间:2017-01-28 作者:kidcoder SAVE UKRAINE

我将此添加到我的函数中。php通过将非管理员重定向到主页来限制对wp admin的非管理员访问。但是,这不适用于非用户(未登录的用户)。

如何在此脚本中包含非用户的访问者?

/**
 * Restrict access to the administration screens.
 *
 * Only administrators will be allowed to access the admin screens,
 * all other users will be automatically redirected to the front of
 * the site instead.
 *
 * We do allow access for Ajax requests though, since these may be
 * initiated from the front end of the site by non-admin users.
 */
function restrict_admin_with_redirect() {
    if ( ! current_user_can( \'manage_options\' ) && ( ! wp_doing_ajax() ) ) {
        wp_redirect( site_url() ); 
        exit;
    }
}
add_action( \'admin_init\', \'restrict_admin_with_redirect\', 1 );

FYI: I created a custom log-in form on a page other than wp-admin so this will not lock out admin

EDIT 2: this is in the functions.php file which may be complicating the issue since when a non-user accesses wp-admin, the server is treating that access as a failed log-in attempt and redirecting the user to the "access-denied" page and not a 404

/**
 * Redirect user on invalid log-in attempts
 */
function login_failed() {
    $login_page  = home_url( \'/access-denied\' );
    wp_redirect( $login_page . \'?login=failed\' );
    exit;
}
add_action( \'wp_login_failed\', \'login_failed\' );

function verify_username_password( $user, $username, $password ) {
    $login_page  = home_url( \'/access-denied\' );
    if( $username == "" || $password == "" ) {
        wp_redirect( $login_page . "?login=empty" );
        exit;
    }
}
add_filter( \'authenticate\', \'verify_username_password\', 1, 3);

2 个回复
SO网友:Shibi

您可以这样做来重定向所有进入wp登录的人。php

请注意,它只在请求为GET时重定向,因此如果您在那里发帖,您仍然可以输入。在您使用此wp登录的情况下。php和您的自定义登录页面。

function login_page_redirect() {
    $redirect_to = "http://google.com"; // url where to redirect
    $page = strtok(basename($_SERVER[\'REQUEST_URI\']), "?");
    if( $page == "wp-login.php" && $_SERVER[\'REQUEST_METHOD\'] == \'GET\') {
        wp_redirect($redirect_to);
        exit;
    }
}
add_action(\'init\',\'login_page_redirect\');

SO网友:T.Todua

只需将其添加到if:

if ( 
     (!current_user_can( \'manage_options\' ) && !wp_doing_ajax() )   
       || 
     (!is_user_logged_in())
   )

相关推荐

无法在模板函数.php中使用IS_HOME

我试图在标题中加载一个滑块,但只在主页上加载。如果有帮助的话,我正在使用Ultralight模板。我正在尝试(在template functions.php中)执行以下操作:<?php if ( is_page( \'home\' ) ) : ?> dynamic_sidebar( \'Homepage Widget\' ); <?php endif; ?> 但这行不通。现在,通过快速的google,我似乎需要将请