| 1 |
<?php |
| 2 |
function forumax_login_form_shortcode( $atts ) { |
| 3 |
// If user already logged in, return a message or redirect |
| 4 |
if ( is_user_logged_in() ) { |
| 5 |
return '<div class="login-already" style="color: green; margin-bottom: 15px;">' . esc_html__( 'You are already logged in.', 'forumax' ) . '</div>'; |
| 6 |
} |
| 7 |
|
| 8 |
// Extract shortcode attributes with defaults |
| 9 |
$atts = shortcode_atts( array( |
| 10 |
'redirect' => '', |
| 11 |
'submit_label' => esc_html__( 'Sign in', 'forumax' ), |
| 12 |
'username_label' => esc_html__( 'Username', 'forumax' ), |
| 13 |
'password_label' => esc_html__( 'Password', 'forumax' ), |
| 14 |
'error_notice' => esc_html__( 'Invalid username or password. Please try again.', 'forumax' ), |
| 15 |
'username_placeholder' => esc_attr__( 'Enter your username', 'forumax' ), |
| 16 |
'password_placeholder' => esc_attr__( 'Enter your password', 'forumax' ), |
| 17 |
'button_color' => '#1e73be', // Default button background color |
| 18 |
'button_text_color' => '#ffffff', // default text color |
| 19 |
'button_position' => 'left', // left, center, right |
| 20 |
'signup_link' => '', // attribute for signup link |
| 21 |
), $atts ); |
| 22 |
|
| 23 |
// Set redirect URL |
| 24 |
$redirect_url = ! empty( $atts['redirect'] ) ? $atts['redirect'] : get_permalink(); |
| 25 |
|
| 26 |
// Determine button alignment based on 'button_position' attribute |
| 27 |
$button_alignment = 'text-align: center;'; // default |
| 28 |
if ( $atts['button_position'] === 'left' ) { |
| 29 |
$button_alignment = 'text-align: left;'; |
| 30 |
} elseif ( $atts['button_position'] === 'right' ) { |
| 31 |
$button_alignment = 'text-align: right;'; |
| 32 |
} |
| 33 |
|
| 34 |
ob_start(); |
| 35 |
|
| 36 |
$form_args = array( |
| 37 |
'redirect' => $redirect_url, |
| 38 |
'label_username' => $atts['username_label'], |
| 39 |
'label_password' => $atts['password_label'], |
| 40 |
'label_remember' => esc_html__( 'Remember Me', 'forumax' ), |
| 41 |
'label_log_in' => $atts['submit_label'], |
| 42 |
'form_id' => 'forumax_login_form', |
| 43 |
'remember' => true, |
| 44 |
'' |
| 45 |
); |
| 46 |
|
| 47 |
wp_login_form( $form_args ); |
| 48 |
|
| 49 |
/** |
| 50 |
* Hook to display social login buttons |
| 51 |
*/ |
| 52 |
do_action( 'forumax_login_form_social_buttons' ); |
| 53 |
|
| 54 |
if ( ! empty( $atts['signup_link'] ) ) : ?> |
| 55 |
<div class="signup-text"> |
| 56 |
<?php esc_html_e( 'Don’t have an account yet?', 'forumax' ); ?> |
| 57 |
<a href="<?php echo esc_url( $atts['signup_link'] ); ?>"> |
| 58 |
<?php esc_html_e( 'Sign up here', 'forumax' ); ?> |
| 59 |
</a> |
| 60 |
</div> |
| 61 |
<?php endif; ?> |
| 62 |
<?php |
| 63 |
|
| 64 |
return ob_get_clean(); |
| 65 |
} |
| 66 |
|
| 67 |
add_shortcode( 'forumax_login_form', 'forumax_login_form_shortcode' ); |
| 68 |
|
| 69 |
// Redirect to the referrer page after login |
| 70 |
add_action( 'wp_login_failed', 'forumax_login_failed_redirect' ); |
| 71 |
|
| 72 |
function forumax_login_failed_redirect( $username ) { |
| 73 |
$referrer = wp_get_referer(); |
| 74 |
if ( ! empty( $referrer ) && ! str_contains( $referrer, 'wp-login.php' ) ) { |
| 75 |
wp_redirect( add_query_arg( 'login', 'failed', $referrer ) ); |
| 76 |
exit; |
| 77 |
} |
| 78 |
} |
| 79 |
|