Home > Software design >  Redirect after login - Wordpress PHP
Redirect after login - Wordpress PHP

Time:11-28

I have a custom login page, which once the user logs in, takes him back to the same page, I would like there to be a redirect on the home page in php, I found this redirect php file, how do I modify it to get users to home?

I include all files that may be of interest

Thanks

stm-lms-user-redirect.php

<?php

$redirect_url = get_site_url();

if(is_user_logged_in()) {
    $lms_settings = get_option('stm_lms_settings', array());
    $user_url = (!empty($lms_settings['user_url'])) ? $lms_settings['user_url'] : '/lms-user';
    $redirect_url .= $user_url . '/' .  get_current_user_id();
}

//wp_safe_redirect($redirect_url);

**stm-lms-user.php**

        <?php
        do_action( 'stm_lms_before_user_header' );
        do_action( 'stm_lms_template_main' );
        
        $current_user = STM_LMS_User::get_current_user( '', true, true );
        $tpl          = 'account/private/main';
        
        stm_lms_register_style( 'user' );
        
        if ( function_exists( 'vc_asset_url' ) ) {
            wp_enqueue_style( 'stm_lms_wpb_front_css', vc_asset_url( 'css/js_composer.min.css' ), array(), time() );
        }
        ?>
        <?php STM_LMS_Templates::show_lms_template( 'modals/preloader' ); ?>
        <div >
        
            <?php do_action( 'stm_lms_admin_after_wrapper_start', $current_user ); ?>
        
            <?php STM_LMS_Templates::show_lms_template( 'account/private/parts/become_instructor_info', compact( 'current_user' ) ); ?>
            <div >
                <?php
                if ( ! empty( $tpl ) ) {
                    STM_LMS_Templates::show_lms_template( $tpl, compact( 'current_user' ) );}
                ?>
            </div>
        </div>

**login.php**

    <div id="stm-lms-login<?php if (isset($form_position)) esc_attr_e($form_position); ?>" 
         v-init="site_key = '<?php echo stm_lms_filtered_output($site_key); ?>'"
         v-bind:>

        <div >
            <?php if (defined('WORDPRESS_SOCIAL_LOGIN_ABS_PATH') and apply_filters('stm_lms_show_social_login', true)) {
                do_action('wordpress_social_login');
            } ?>

            <h3><?php esc_html_e('Login', 'masterstudy-lms-learning-management-system'); ?></h3>

            <?php do_action('stm_lms_login_end'); ?>
        </div>

        <div >

            <div >
                <label >
                    <?php echo apply_filters('stm_lms_login_label', esc_html__('Username', 'masterstudy-lms-learning-management-system')); ?>
                </label>
                <input 
                       type="text"
                       name="login"
                       v-model="login"
                       v-on:keyup.enter="logIn()"
                       placeholder="<?php esc_html_e('Enter username', 'masterstudy-lms-learning-management-system'); ?>"/>
            </div>

            <div >
                <label >
                    <?php echo apply_filters('stm_lms_password_label', esc_html__('Password', 'masterstudy-lms-learning-management-system')); ?>
                </label>
                <input 
                       type="password"
                       name="password"
                       v-model="password"
                       v-on:keyup.enter="logIn()"
                       placeholder="<?php esc_html_e('Enter password', 'masterstudy-lms-learning-management-system'); ?>"/>
            </div>

            <div >

                <label >
                <span >
                    <input
                            type="checkbox"
                            name="remember_me"
                            v-model="remember"
                            v-on:keyup.enter="logIn()"
                    />
                    <span><i ></i> </span>
                </span>
                    <span><?php esc_html_e('Remember me', 'masterstudy-lms-learning-management-system'); ?></span>
                </label>

                <span 
                      @click.prevent="open_lost_password = !open_lost_password"
                      title="<?php esc_html_e('Lost Password', 'masterstudy-lms-learning-management-system'); ?>">
                <?php esc_html_e('Lost Password', 'masterstudy-lms-learning-management-system'); ?>
            </span>

                <a href="#"
                   
                   v-bind:
                   @click.prevent="logIn()">
                    <span><?php echo _x('Login', 'Login button', 'masterstudy-lms-learning-management-system'); ?></span>
                </a>

            </div>

            <div  v-if="open_lost_password">

                <div >
                    <label >
                        <?php echo apply_filters('stm_lms_lost_password_label', esc_html__('Login/E-mail', 'masterstudy-lms-learning-management-system')); ?>
                    </label>
                    <input 
                           type="text"
                           name="login"
                           v-model="lost_password"
                           placeholder="<?php esc_html_e('Enter login/e-mail', 'masterstudy-lms-learning-management-system'); ?>"/>
                </div>

                <a href="#"
                   
                   v-bind:
                   @click.prevent="lostPassword()">
                    <span><?php esc_html_e('Send', 'masterstudy-lms-learning-management-system'); ?></span>
                </a>

            </div>

        </div>

        <transition name="slide-fade">
            <div  v-bind: v-if="message" v-html="message">
            </div>
        </transition>

    </div>

<?php if (defined('APSL_VERSION') and apply_filters('stm_lms_show_social_login', true)) {
    echo do_shortcode("[apsl-login-lite login_text='']");
} ?>

<?php if (defined('NSL_PATH_FILE') and apply_filters('stm_lms_show_social_login', true)) {
    echo do_shortcode('[nextend_social_login]');
} ?>

<?php do_action('stm_lms_login_section_end'); ?>

I have tried to modify the code without success... I add "header('Location: https://url/');" in stm-lms-user-redirect.php but not work.

I have a custom login page, which once the user logs in, takes him back to the same page, I would like there to be a redirect on the home page in php, I found this redirect php file, how do I modify it to get users to home?

I include all files that may be of interest

Thanks

CodePudding user response:

This can be accomplished using the template_redirect hook. The hook is triggered when the user is logged in and only visiting the login.php page.

Put the below code in your functions.php file. And let me know if it works for you or not.

add_action('template_redirect', 'kb_logged_in_redirect');
function kb_logged_in_redirect()
{
    if (is_page_template('login.php') && is_user_logged_in()) {
        $redirect_to = home_url();
        wp_safe_redirect($redirect_to);
        exit();
    }
}

CodePudding user response:

you have to redirect before the header is sent to the browser. so write your condition code under the 'template_redirect' hook. so in your stm-lms-user-redirect.php file write something like this

add_filter('template_redirect', 'your_redirection_function', 10);
function your_redirection_function()
{
    $redirect_url = get_site_url();
    if (is_user_logged_in() && isset($_GET['send_some_get_perameter_from_form'])) {
        $lms_settings = get_option('stm_lms_settings', array());
        $user_url = (!empty($lms_settings['user_url'])) ?
            $lms_settings['user_url'] : '/lms-user';
        $redirect_url .= $user_url . '/' . get_current_user_id();
    }
}

About $_GET['send_some_get_perameter_from_form'] it is important to send. otherwise, the whole website will always be redirected to the $redirect_url page. you can include your login.php in a page template and you can write your code like this

add_filter('template_redirect', 'your_redirection_function', 10);
function your_redirection_function()
{
    $redirect_url = get_site_url();
    if (is_user_logged_in() && is_page_template( 'your-page-template.php' );) {
        $lms_settings = get_option('stm_lms_settings', array());
        $user_url = (!empty($lms_settings['user_url'])) ?
            $lms_settings['user_url'] : '/lms-user';
        $redirect_url .= $user_url . '/'. get_current_user_id();
    }
}
  • Related