Redirect to my own login and registration form when the user is not logged in

I have searched and ended up finding a snippet on github -

add_filter(
	'hivepress/v1/forms/user_login',
	function( $form ) {
		if ( ! isset( $form['redirect'] ) )  {
			$form['redirect'] = 'mysiteurl/login';
		}

		return $form;
	}
);

I am trying to have my account page redirect to my own login and registration form when the user is not logged in. Not rediret to (what i believe is a hivepress login form. I think it is hivepress as when i inspect in the browser this is what the element shows as:

'form class=“hp-form–narrow hp-block hp-form hp-form–user-login” data-model=“user” action=“#” data-action=“mysiteurl/wp-json/hivepress/v1/users/login/” method=“POST” data-redirect=“myURL/login” data-component=“form”>

Username or Email
Password
Sign In
</......form'

Is there a way to do this? I want to redirect to my own login page?

Hi! If I understand correctly, you want to redirect users to your custom register page, when they clicked on login, and the login modal’s bottom “Don’t have an account yet? Register” button? Because currently this is the only one option as I know, what call the register modal “#user_register_modal”. On my website I have a same custom register page, and I created a header menu button for that, so it’s easly visible for non registered users. But if a user clicking on that modal “Register” button, you can override that template easily.

  1. Create this folders, and the file (user-register-link.php) in your theme/child theme library:
    hivepress/user/login/user-register-link.php
public_html/
└── wp-content/
    └── themes/
        └── your-child-theme/
            └── hivepress/
                └── user/
                    └── login/
                        └── user-register-link.php

add this code into that file:

<?php
// Exit if accessed directly.
defined( 'ABSPATH' ) || exit;

if ( get_option( 'hp_user_enable_registration', true ) ) :
	?>
	<p class="hp-form__action hp-form__action--user-register"><?php esc_html_e( 'Don\'t have an account yet?', 'hivepress' ); ?> <a href="https://yourwebsite.com/registerpage/"><?php esc_html_e( 'Register', 'hivepress' ); ?></a></p>
	<?php
endif;
?>

Replace this: “https://yourwebsite.com/registerpage/” to your register page url.
Save, and if you have any caching plugin or solution, purge cache, because this will not work!

If you want to redirect when they click on the HivePress login button, it’s a same procedure, just create this file, in the same folder what you created: “user-login-link.php” and copy this into that file:

<?php
if ( is_user_logged_in() ) :
	?>
	<a href="<?php echo esc_url( hivepress()->router->get_url( 'user_account_page' ) ); ?>" class="hp-menu__item hp-menu__item--user-account hp-link">
		<i class="hp-icon fas fa-user"></i>
		<span><?php echo esc_html( hivepress()->request->get_user()->get_username() ); ?></span>
		<?php if ( hivepress()->request->get_context( 'notice_count' ) ) : ?>
			<small><?php echo esc_html( hivepress()->request->get_context( 'notice_count' ) ); ?></small>
		<?php endif; ?>
	</a>
<?php elseif ( get_option( 'hp_user_enable_registration', true ) ) : ?>
	<a href="https://yourwebsite.com/loginpage/" class="hp-menu__item hp-menu__item--user-login hp-link">
		<i class="hp-icon fas fa-sign-in-alt"></i>
		<span><?php esc_html_e( 'Sign In', 'hivepress' ); ?></span>
	</a>
	<?php
endif;	

Don’t forget to purge cache.

3 Likes

Thank you for the code. It worked perfectly.

Only other thing I had to was to change the link login link on the Register page, too. Otherwise, it was still opening the login modal.

I did that by creating .../child-theme/hivepress/user/register/ and copying the user-login-link.php file from /wp-content/plugins/hivepress/templates/user/register to my child theme and updating the URL.

Thanks so much for the detailed reply! It really helped me out :+1:t3:

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.