0

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

1 Answer 1

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.

3
  • Hello @syslogic I've tried both and it doesn't work. Also uninstalled the plugin and reinstalled it. May 4, 2016 at 10:00
  • @Kevin if the database was updated, at least the urls should work as expected... that ob_start() without end/flush seems strange. that home_url('wp-admin') might be called, while not having set the auth cookie yet... the resulting url looks just like that. May 4, 2016 at 23:16
  • I don't know what it is, but I've tried other login forms and the problem still there. Must be something else. May 11, 2016 at 15:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.