重定向客户而不是管理员的WordPress登录

时间:2021-07-16 作者:Philip Deatherage

我知道如何从/wp登录重定向用户。然而,php在排除来自/wp admin/(重定向到wp-login.php)的人时遇到了一个问题。有没有办法排除这些人?这是我当前的代码:

function prevent_wp_login() {
    // WP tracks the current page - global the variable to access it
    global $pagenow;
    // Check if a $_GET[\'action\'] is set, and if so, load it into $action variable
    $action = (isset($_GET[\'action\'])) ? $_GET[\'action\'] : \'\';
    $args = (isset($_GET[\'args\'])) ? $_GET[\'args\'] : \'\';
    // Check if we\'re on the login page, and ensure the action is not \'logout\'
    if( $pagenow == \'wp-login.php\' && ( ! $action || ( $action && ! in_array($action, array(\'logout\', \'lostpassword\', \'rp\', \'resetpass\')))) && ( ! $args || ( $args && ! in_array($args, array(\'reauth\', \'redirect\')))) ) {
        // Load the home page url
        // Redirect to the home page
        wp_redirect(\'/my-account/\');
        // Stop execution to prevent the page loading for any reason
        exit();
    }
}

1 个回复
SO网友:Philip Deatherage

经过一点思考,我想出了一个允许wp登录的解决方案。php将为公众/客户重定向,但不为管理员重定向。我通过获取推荐人的URL来实现这一点。这是我的更新代码,但我对其他想法持开放态度。

//Redirect for login from wp-login.php to my-account if not admin
add_action(\'init\', \'prevent_wp_login\');
function prevent_wp_login() {
    // WP tracks the current page - global the variable to access it
    global $pagenow;
    // Check if a $_GET[\'action\'] is set, and if so, load it into $action variable
    $action = (isset($_GET[\'action\'])) ? $_GET[\'action\'] : \'\';
    
    //check if we came from the admin page or wp-admin
    $refer = urlencode($_SERVER["REQUEST_URI"]);
    if (strpos($refer, \'wp-admin\') !== false) {
        // stay on wp-login.php
    } else {
        // Check if we\'re on the login page, and ensure the action is not \'logout\'
        if( $pagenow == \'wp-login.php\' && ( ! $action || ( $action && ! in_array($action, array(\'logout\', \'lostpassword\', \'rp\', \'resetpass\'))))) {
            // Load the home page url
            // Redirect to the home page
            wp_redirect(\'/my-account/\');
            // Stop execution to prevent the page loading for any reason
            exit();
        }
    }
}

相关推荐

WordPress更新-这通常是由于文件权限不一致。例如:wp-admin/includes/update-core.php

我有一些具有根访问权限的WordPress网站服务器,在创建服务器时,一切都很好,WordPress本身以及插件和主题都在更新,没有出现任何问题,但我不知道为什么最近我遇到了一个错误,通常指示权限错误(至少我看到权限是正确的)。更新WordPress时出错:正在从下载更新https://downloads.wordpress.org/release/fa_IR/wordpress-5.7.zip…wordpress-5.7的真实性。无法验证zip,因为找不到签名。正在解压缩更新…无法安装更新,因为我们将无