Diverting WordPress login and register links to Jo

2019-08-22 18:28发布

I have a Joomla plugin which does a one-way single sign-on between Joomla and WordPress.

It means the registrations and logins are handled by Joomla and every time someone registers in Joomla it replicates the user's data into WordPress user tables and everytime someone logs into Joomla it writes the user's status as logged in into the WordPress cookie thus the user automatically gets logged into WordPress.

However this is not the case with WordPress. It will not do these actions if someone logs into WordPress or registers into WordPress.

Hence I want to divert users from Wordpress to Joomla pages for login and registration activities.

Example:

Joomla login page: http://www.xyz.com/index.php?option=com_user&view=login&Itemid=204

Joomla registration page: http://www.xyz.com/index.php?option=com_user&view=login&Itemid=205

Now I want that when a user clicks on the login link in the default Meta Widget it must take the user to the above Joomla page and similarly when the user clicks on Register, it must take the user to the above registration page.

I am looking for a solution preferably without hacking the core files. Please feel free to suggest if there is a better solution then what I am looking for above.

I use the Atom theme and it has a built-in login form widget, and the code for that widget is:

Please note: I am a novice when it comes to programming hence requesting a detailed reply.

/**
 * Login widget
 * based on the "Login with AJAX" plugin - http://netweblogic.com/wordpress/plugins/login-with-ajax
 *
 * @since 1.0
 * @todo add Register Form
 */
class atom_widget_login extends WP_Widget{

  function atom_widget_login(){
    $widget_ops = array('description' => __("Login and Lost Password forms", ATOM));
    $control_ops = array('width' => 500);
    $this->WP_Widget(false, __("Login", ATOM), $widget_ops, $control_ops);
    add_action('init', array(&$this, 'ajax'), 99);

    // include in jQuery(document).ready()
    add_action('atom_jquery_init', array(&$this, 'js'));
  }

  function js(){
    // We need to process all instances because this function gets to run only once.
    $widget_settings = get_option($this->option_name);

    foreach((array)$widget_settings as $instance => $options):

      // Identify instance
      $id = $this->id_base.'-'.$instance;
      $block_id = 'instance-'.$id;

      if (is_active_widget(false, $id, $this->id_base)): ?>

      $('#<?php echo $id; ?>_login').submit(function(event){

        // Stop event
        event.preventDefault();

        $status = $("#<?php echo $block_id; ?> .status");
        $status.removeClass("error").addClass("loading").text("<?php _e("Checking...", ATOM); ?>");

        // Sort out URL
        url = $('#<?php echo $id; ?>_login').attr('action');
        url += (url.match(/\?/) != null) ? '&callback=?' : '?callback=?' ;
        url += "&log="+encodeURIComponent($("#<?php echo $id; ?>_user").val());
        url += "&pwd="+encodeURIComponent($("#<?php echo $id; ?>_pass").val());
        url += "&rememberme="+encodeURIComponent($("#<?php echo $id; ?>_login_remember").val());
        url += "&login=login";

        $.getJSON(url, function(data, status){
          if(data.result === true || data.result === false){
            if(data.result === true){
              $status.removeClass("loading error").addClass("success").html(data.message);
              window.location.reload();
            }else{
              $status.removeClass("loading").addClass("error").html(data.error);

              // Assume the link in the status message points to forgot pass form.
              $status.find("a").click(function(event){
                event.preventDefault();

                if($("#<?php echo $id; ?>_forgot").is(":visible")){
                  var origColor = $("#<?php echo $id; ?>_forgot input.text").css("color");
                  $("#<?php echo $id; ?>_forgot input.text").css({backgroundColor: '#ffa799', color: '#333'}).animate({backgroundColor: '#fff', color: origColor}, 1000);
                }else{
                  $("#<?php echo $block_id; ?> a.forgot_pass").remove(); // remove the bottom forgot pass link
                  $('#<?php echo $id; ?>_forgot').slideFade('toggle',333,'easeOutQuart');
                }

              });
            }
          }else{
            $status.removeClass("loading").html("<?php _e("Unkown error. Please try again...", ATOM); ?>");
          }
        });
      });

      $('#<?php echo $id; ?>_forgot').submit(function(event){

        // Stop event
        event.preventDefault();

        $status = $("#<?php echo $block_id; ?> .status");
        $status.removeClass("error").addClass("loading").text("<?php _e("Checking...", ATOM); ?>");

        // Sort out URL
        url = $('#<?php echo $id; ?>_forgot').attr('action');
        url += (url.match(/\?/) != null) ? '&callback=?' : '?callback=?' ;
        url += "&user_login="+$("#<?php echo $id; ?>_user_or_email").val();
        url += "&login=forgot_pass";

        $.getJSON(url, function(data, status){
          if(data.result === true || data.result === false){
            if(data.result == '1') $status.removeClass("loading error").addClass("success").html(data.message);
            else $status.removeClass("loading").addClass("error").html(data.error);
          }else{
            $status.removeClass("loading").addClass("error").html("<?php _e("Unkown error. Please try again...", ATOM); ?>");
          }
        });
      });

      $("#<?php echo $block_id; ?> a.forgot_pass").click(function(event){
        event.preventDefault();
        $(this).remove();
        $('#<?php echo $id; ?>_forgot').slideFade('toggle',333,'easeOutQuart');
      });

      <?php endif;
    endforeach;
  }

  function ajax(){

    if(isset($_GET["login"])):
      switch($_GET["login"]):
        case 'login':
          $_POST['log'] = $_GET['log'];
          $_POST['pwd'] = $_GET['pwd'];
          $_POST['rememberme'] = $_GET['rememberme'];

          global $current_user;
          $return = array(); //What we send back
          $loginResult = wp_signon();

          if(strtolower(get_class($loginResult)) == 'wp_user'):
            //User login successful
            $current_user = $loginResult;
            /* @var $loginResult WP_User */
            $return['result'] = true;
            $return['message'] = __("Login Successful, redirecting...", ATOM);

          elseif(strtolower(get_class($loginResult)) == 'wp_error'):
            //User login failed
            /* @var $loginResult WP_Error */
            $return['result'] = false;
            $error = $loginResult->get_error_message();
            $return['error'] = ($error ? $error : __("Please type a username and password", ATOM));

          else:
            //Undefined Error
            $return['result'] = false;
            $return['error'] = __('Unknown error. Sorry...', ATOM);

          endif;
          $return = json_encode($return);
        break;

        case 'forgot_pass':
          $_POST['user_login'] = $_GET['user_login'];

          // Reads ajax login creds via POSt, calls the login script and interprets the result
          $remember = array(); //What we send back
          $result = retrieve_password();

          if($result === true):
            //Password correctly remembered
            $remember['result'] = true;
            $remember['message'] = __("E-mail has been sent, check your inbox.", ATOM);
          elseif(strtolower(get_class($result)) == 'wp_error'):
            //Something went wrong
            /* @var $result WP_Error */
            $remember['result'] = false;
            $remember['error'] = $result->get_error_message();
          else:
            //Undefined Error
            $remember['result'] = false;
            $remember['error'] = __('Unknown error. Sorry...', ATOM);
          endif;

          $return = json_encode($remember);
        break;

        default:
          $return = json_encode(array('result' => 0, 'error' => __('Requested command is invalid', ATOM)));
        break;
      endswitch;
      if(isset($_GET['callback'])) $return = $_GET['callback']."($return)";
      echo $return;
      exit();
    endif;
  }

  function widget($args, $instance){
    extract($args);
    $title = apply_filters('widget_title', $instance['title'], $instance, $this->id_base);

    // Retrieve information about the current user.
    global $current_user, $user_level;
    get_currentuserinfo();

    if(is_user_logged_in()) $title = sprintf(__('Welcome %s', ATOM), $current_user->display_name);
    echo $before_widget.($title ? $before_title.$title.$after_title : null);

    echo '<div class="box login-block clear-block">';

    // the user is logged in, display the menu links
    if(is_user_logged_in()):
      echo '<div class="avatar">'.atom_get_avatar($current_user->user_email, 96, '', $current_user->display_name).'</div>';
      echo '<ul class="menu">';

      if($instance['dashboard']) echo '<li class="first"><a class="fadeThis" href="'.admin_url().'">'.__('Dashboard', ATOM).'</a></li>';

      if($user_level >= 1): // need permissions
        if($instance['write'])  echo '<li><a class="fadeThis" href="'.admin_url('post-new.php').'">'.__('Write', ATOM).'</a></li>';
        if($instance['comments']) echo '<li><a class="fadeThis" href="'.admin_url('edit-comments.php').'">'.__('Comments', ATOM).'</a></li>';
      endif;

      if($instance['profile']) echo '<li><a class="fadeThis" href="'.admin_url('profile.php').'">'.__('Profile', ATOM).'</a></li>';
      echo '<li><a class="fadeThis last" id="wp-logout" href="'.wp_logout_url(atom_get_current_page_url()).'">'.__('Log Out', ATOM).'</a></li>';
      echo '</ul>';

    // The user is not logged in, display the login form
    else: ?>

      <div class="status clear-block"><?php echo $instance['text']; ?></div>

      <form id="<?php echo $this->id; ?>_login" action="<?php echo site_url('wp-login.php', 'login_post') ?>" method="post">

        <div>
          <input type="text" rel="<?php echo __("User", ATOM); ?>" name="log" id="<?php echo $this->id; ?>_user" class="text clearField" value="" />
          <input type="password" rel="<?php echo __("Password", ATOM); ?>" name="pwd" id="<?php echo $this->id; ?>_pass" class="text clearField" value="" />
        </div>

        <div class="clear-block">
          <input type="submit" name="wp-submit" class="alignleft" value="<?php _e('Log In', ATOM); ?>" tabindex="100" />

          <input type="hidden" name="redirect_to" value="<?php echo atom_get_current_page_url(); ?>" />
          <input type="hidden" name="testcookie" value="1" />
          <input type="hidden" name="login" value="login" />

          <label for="<?php echo $this->id; ?>_login_remember" class="remember alignleft">
            <input name="rememberme" type="checkbox" id="<?php echo $this->id; ?>_login_remember" value="forever" />
            <?php _e('Remember me', ATOM); ?>
          </label>
        </div>
      </form>

      <form id="<?php echo $this->id; ?>_forgot" action="<?php echo site_url('wp-login.php?action=lostpassword', 'login_post') ?>" method="post" class="hidden">
        <div>
          <input type="text" name="user_login" size="20" id="<?php echo $this->id; ?>_user_or_email" class="text wide clearField" value="<?php _e("Enter username or email", ATOM); ?>" />
          <input type="submit" value="<?php echo __("Get new password", ATOM); ?>" />
          <input type="hidden" name="login" value="forgot_pass" />
        </div>
      </form>

      <?php
      echo '<a class="forgot_pass" href="'.site_url('wp-login.php?action=lostpassword', 'login').'">'.__('Lost your password?', ATOM).'</a>';

      if (get_option('users_can_register')):
        if(function_exists('bp_get_signup_page')) $register_link = bp_get_signup_page(); // bp
        elseif(file_exists(ABSPATH."/wp-signup.php")) $register_link = site_url('wp-signup.php', 'login'); //MU + WP3
        else $register_link = site_url('wp-login.php?action=register', 'login');
        echo '<a class="register" href="'.$register_link.'">'.__('Register', ATOM).'</a>';
      endif;

   endif;

    echo '</div>';
 echo $after_widget;
  }

  function update($new_instance, $old_instance){
    $instance['title'] = esc_attr($new_instance['title']);
    if (current_user_can('unfiltered_html')) $instance['text'] =  $new_instance['text'];
    else $instance['text'] = stripslashes(wp_filter_post_kses(addslashes($new_instance['text']))); // wp_filter_post_kses() expects slashed
    $instance['dashboard'] = isset($new_instance['dashboard']);
    $instance['profile'] = isset($new_instance['profile']);
    $instance['write'] = isset($new_instance['write']);
    $instance['comments'] = isset($new_instance['comments']);
    return $instance;
  }

  function form($instance){
    $instance = wp_parse_args((array)$instance, apply_filters('atom_widget_login_defaults', array(
      'title' => __('Log in', ATOM),
      'text' => __("Hello Guest. Login below if you have a account", ATOM),
      'dashboard' => 1,
      'profile' => 1,
      'write' => 1,
      'comments' => 0)));
    ?>

    <p>
      <label for="<?php echo $this->get_field_name('title'); ?>"><?php _e('Title:', ATOM); ?>
      <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php if (isset($instance['title'])) echo esc_attr($instance['title']); ?>" /></label>
    </p>

    <p>
      <label for="<?php echo $this->get_field_name('text'); ?>"><?php _e('Initial Status Text (or HTML):', ATOM); ?>
      <textarea class="widefat code" id="<?php echo $this->get_field_id('text'); ?>" name="<?php echo $this->get_field_name('text'); ?>" rows="6" cols="28"><?php if (isset($instance['text'])) echo format_to_edit($instance['text']); ?></textarea>
      </label>
    </p>

    <p><strong><em><?php _e("Welcome screen links (if enough permissions):", ATOM); ?></em></strong></p>
    <p>
     <label for="<?php echo $this->get_field_id('dashboard'); ?>">
       <input id="<?php echo $this->get_field_id('dashboard'); ?>" name="<?php echo $this->get_field_name('dashboard'); ?>" type="checkbox" value="1" <?php checked(isset($instance['dashboard']) ? $instance['dashboard'] : 0); ?> />
       <?php _e('Dashboard', ATOM); ?>
     </label>
     <br />

     <label for="<?php echo $this->get_field_id('profile'); ?>">
       <input id="<?php echo $this->get_field_id('profile'); ?>" name="<?php echo $this->get_field_name('profile'); ?>" type="checkbox" value="1" <?php checked(isset($instance['profile']) ? $instance['profile'] : 0); ?> />
       <?php _e('Profile', ATOM); ?>
     </label>
     <br />

     <label for="<?php echo $this->get_field_id('write'); ?>">
       <input id="<?php echo $this->get_field_id('write'); ?>" name="<?php echo $this->get_field_name('write'); ?>" type="checkbox" value="1" <?php checked(isset($instance['write']) ? $instance['write'] : 0); ?> />
       <?php _e('Write', ATOM); ?>
     </label>
     <br />

     <label for="<?php echo $this->get_field_id('comments'); ?>">
       <input id="<?php echo $this->get_field_id('comments'); ?>" name="<?php echo $this->get_field_name('comments'); ?>" type="checkbox" value="1" <?php checked(isset($instance['comments']) ? $instance['comments'] : 0); ?> />
       <?php _e('Comments', ATOM); ?>
     </label>
     <br />

     <label>
       <input disabled="disabled" type="checkbox" value="1" checked="checked" />
       <?php _e('Log out', ATOM); ?>
     </label>
    </p>
    <?php
  }
}

1条回答
爷、活的狠高调
2楼-- · 2019-08-22 19:03

Replace this

<form id="<?php echo $this->id; ?>_login" action="<?php echo site_url('wp-login.php', 'login_post') ?>" method="post">

with

<form id="<?php echo $this->id; ?>_login" action="<?php echo 'http://www.xyz.com/index.php?option=com_user&view=login&Itemid=204' ?>" method="post">

for logging in and for registering link replace

$register_link = site_url('wp-login.php?action=register', 'login');

with

$register_link = 'http://www.xyz.com/index.php?option=com_user&view=login&Itemid=205';

P.S - Didn't try it myself

查看更多
登录 后发表回答