Custom Login Plugin Redirects to wp-login.php After Site Migration

admin2025-01-07  3

I am using a custom login plugin I made for my site, it was working fine before migration but now it redirects to the wp-login.php page.

Old Site -> / (still available and working)

New site ->

When I try to access the new site I get redirected here: .php?redirect_to=https%3A%2F%2Fwww.buscopreparador%2Fwp-admin%2F&reauth=1

This is my plugin code:

<?php
/*
Plugin Name: Login BP
Plugin URI: 
Description: Login para BuscoPreparador
Version: 1.0
Author: Kevin Mamaqi
Author URI: 
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 Changing wp_redirect( home_url( 'wp-admin' ) ); to wp_redirect( home_url() ); works. And makes me wonder if this function to redirect based on user roles has something to do, but not sure:

/**
 * 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 );

I am using a custom login plugin I made for my site, it was working fine before migration but now it redirects to the wp-login.php page.

Old Site -> http://prep.kevinmamaqi.com/ (still available and working)

New site -> https://www.buscopreparador.com

When I try to access the new site I get redirected here: https://www.buscopreparador.com/wp-login.php?redirect_to=https%3A%2F%2Fwww.buscopreparador.com%2Fwp-admin%2F&reauth=1

This is my plugin code:

<?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 Changing wp_redirect( home_url( 'wp-admin' ) ); to wp_redirect( home_url() ); works. And makes me wonder if this function to redirect based on user roles has something to do, but not sure:

/**
 * 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 );
Share Improve this question edited Jun 15, 2020 at 8:21 CommunityBot 1 asked May 4, 2016 at 7:57 Kevin MamaqiKevin Mamaqi 3881 gold badge8 silver badges21 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

you'll need to update two records within the wp_options table; this can even be done via PHP:

update_option('siteurl', 'https://www.buscopreparador.com');
update_option('home',    'https://www.buscopreparador.com');

when these lines are put into the functions.php of the theme and the page had been loaded once, the database should match the current hostname - after that happened, they should be removed again (because they become useless, once the database had been updated).

that Google reCAPTCHA sitekey would also need to be updated, according to the current hostname.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736252408a43.html

最新回复(0)