自定义登录插件在站点迁移后重定向到wp-login.php

时间:2016-05-04 作者:Kevin Mamaqi

我正在使用我为我的网站制作的自定义登录插件,在迁移之前它工作正常,但现在它重定向到wp登录。php页面。

旧站点->;http://prep.kevinmamaqi.com/ (仍可用且正在工作)

新建站点->;https://www.buscopreparador.com

当我尝试访问新网站时,我会在此处重定向:https://www.buscopreparador.com/wp-login.php?redirect_to=https%3A%2F%2Fwww.buscopreparador.com%2Fwp-admin%2F&reauth=1

这是我的插件代码:

<?php
/*
Plugin Name: Login BP
Plugin URI: https://www.kevinmamaqi.com
Description: Login para BuscoPreparador.com
Version: 1.0
Author: Kevin Mamaqi
Author URI: https://www.kevinmamaqi.com
License: GPL2
*/

/**
 * Adds Login_BP widget.
 */

// Turn on output buffering
ob_start();


class BP_Login extends WP_Widget {

    static private $login_registration_status;

    /**
     * Register widget with WordPress.
     */
    function __construct() {
        parent::__construct(
            \'bp_login\', // Base ID
            __( \'BP_Login\', \'text_domain\' ), // Name
            array( \'description\' => __( \'A tabbed login and registration widget for WordPress\', \'text_domain\' ), ) // Args
        );
    }

    /**
     * Returns the HTML for the login form
     * @return string
     */
    static function login_form() {
        $html = \'<form method="post" action="\' . esc_url( $_SERVER[\'REQUEST_URI\'] ) . \'">\';
        $html .= \'<div class="form-group"><input type="text" class="form-control" name="login_username" id="login_username" placeholder="Nombre de Usuario"></div>\';
        $html .= \'<div class="form-group"><input type="password" class="form-control" name="login_password" id="login_password" placeholder="Contraseña"></div>\';
        $html .= \'<div class="g-recaptcha" data-sitekey="6LefKhoTAAAAALBduf1bC_0TvopN2aUGU0X5rcos"></div>\';
        $html .= \'<div class="checkbox"><label><input type="checkbox" name="remember_login" value="true" checked="checked"> Recuerdame</label></div>\';
        $html .= \'<input class="btn btn-primary btn-block" type="submit" name="login_submit" value="Acceder" />\';
        $html .= \'<li role="separator" style="margin: 1.5em 0;" class="divider"></li>\';
        $html .= \'<p>¿Todavía no estas registrado?</p>\';
        $html .= \'<a class="btn btn-warning btn-block" href="\' . wp_registration_url() . \'">Únete</a>\';
        $html .= \'</form>\';

        return $html;

    }


    /**
     * Login registered users
     */
    function login_user() {
        if ( isset( $_POST[\'login_submit\'] ) ) {

            $creds                  = array();
            $creds[\'user_login\']    = esc_attr( $_POST[\'login_username\'] );
            $creds[\'user_password\'] = esc_attr( $_POST[\'login_password\'] );
            $creds[\'remember\']      = esc_attr( $_POST[\'remember_login\'] );

            $login_user = wp_signon( $creds, false );

            if ( ! is_wp_error( $login_user ) ) {
                wp_redirect( home_url( \'wp-admin\' ) );
            } elseif ( is_wp_error( $login_user ) ) {
                self::$login_registration_status = $login_user->get_error_message();
            }
        }
    }


    public function widget( $args, $instance ) { ?>

        <?php
        $title = apply_filters( \'widget_title\', $instance[\'title\'] );

        echo $args[\'before_widget\'];
        if ( ! empty( $title ) ) {
            echo $args[\'before_title\'] . $title . $args[\'after_title\'];
        } ?>

        <?php $this->login_user(); ?>

        <div class="login-reg-error"><?php echo self::$login_registration_status; ?></div>

        <?php echo self::login_form(); ?>

        <?php
        echo $args[\'after_widget\'];
    }


    public function form( $instance ) {
        if ( isset( $instance[\'title\'] ) ) {
            $title = $instance[\'title\'];
        } else {
            $title = __( \'Acceso BP\', \'text_domain\' );
        }
        ?>
        <p>
            <label for="<?php echo $this->get_field_id( \'title\' ); ?>"><?php _e( \'Title:\' ); ?></label>
            <input class="widefat" id="<?php echo $this->get_field_id( \'title\' ); ?>"
                   name="<?php echo $this->get_field_name( \'title\' ); ?>" type="text"
                   value="<?php echo esc_attr( $title ); ?>">
        </p>
    <?php
    }

    public function update( $new_instance, $old_instance ) {
        $instance          = array();
        $instance[\'title\'] = ( ! empty( $new_instance[\'title\'] ) ) ? strip_tags( $new_instance[\'title\'] ) : \'\';

        return $instance;
    }

} // class BP_Login

// register Foo_Widget widget
function register_bp_login() {
    register_widget( \'BP_login\' );
}

add_action( \'widgets_init\', \'register_bp_login\' );
UPDATE正在更改wp_redirect( home_url( \'wp-admin\' ) );wp_redirect( home_url() ); 作品我想知道这个基于用户角色重定向的函数是否有什么作用,但不确定:

/**
 * Redirect user after successful login.
 *
 * @param string $redirect_to URL to redirect to.
 * @param string $request URL the user is coming from.
 * @param object $user Logged user\'s data.
 * @return string
 */
function my_login_redirect( $redirect_to, $request, $user ) {
  //is there a user to check?
  if ( isset( $user->roles ) && is_array( $user->roles ) ) {
    //check for admins
    if ( in_array( \'administrator\', $user->roles ) ) {
      // redirect them to the default place
      return $redirect_to;

    } else {
      return home_url(\'mi-perfil\');
    }
  } else {
    return $redirect_to;
  }
}

add_filter( \'login_redirect\', \'my_login_redirect\', 10, 3 );

1 个回复
SO网友:Martin Zeitler

您需要更新wp\\u options表中的两条记录;这甚至可以通过PHP完成:

update_option(\'siteurl\', \'https://www.buscopreparador.com\');
update_option(\'home\',    \'https://www.buscopreparador.com\');
将这些行放入函数时。主题和页面的php已加载一次,数据库应与当前主机名匹配-之后,应再次删除它们(因为一旦数据库更新,它们将变得无用)。

根据当前的主机名,Google reCAPTCHA sitekey也需要更新。

相关推荐

Redirect Css files to https

在哪里可以找到负责调用Css文件的文件,并测试它们的链接是http还是https?我想测试请求是否为https,如果是,那么很好,如果不是,请将其更改为https在函数中。php我找到了负责调用主题Css的函数,下面是它的代码:if (!function_exists(\'jobcareer_front_scripts\')) { function jobcareer_front_scripts() { global $jobcareer_options;