I have a menu item "Checkout". When clicked it links to the login popup prompting users to register/login/continue as guest and from there to checkout. How can I make that same button redirect to the checkout page if a user is already logged in? Using ELementor and Astra theme Thanks!
-
What have you tried so far?– Tony DjukicJan 9, 2021 at 4:36
-
Welcome to WPSE. I suggest you add more of context to your question and explain what you already tried, or at least the strategies you are thinking to use, to improve the odds of a useful answer and to comply to what is expected in the community. I also suggest you take a look at [How do I ask a good question?](wordpress.stackexchange.com/help/how-to-ask), the Code of Conduct and other articles in the help section: wordpress.stackexchange.com/help– Celso BessaJan 9, 2021 at 15:07
-
I am new to this, I guess I need to add a php snippet, right? Basically, when clicking the menu item check if the user is logged in if false rediret to login popup if true redirect to checkout.– Ricardo ArnedoJan 9, 2021 at 15:59
2 Answers
In a more "vanilla" setting (i.e. without a page builder and not using a modal or popup) you would have two basic approaches:
- a) generating a different menu
- b) redirecting from the target page
I would not generate a different menu because, in my experience, it seems a little more messy and less performant depending on the server and WordPress settings. I would use option B and I would not use the login in a popup/modal, using a function along the lines of the code you shared, but with a few tweaks:
add_action( 'template_redirect', 'wpse381189_dashboard_redirect' );
function wpse381189_dashboard_redirect() {
// make sure i) we are not in admin side ii) we are not doing ajax iii) user is logged in.
if( !is_admin() && !defined( 'DOING_AJAX' )&& is_user_logged_in() ) {
nocache_headers();
wp_safe_redirect( home_url( '/dashboard/' ) );
exit();
}
}
If you still want to use a popup/modal, you would need to use a function that returns data telling the frontend the user status (i.e. logged in or logged out) and let the Javascript handle the redirection. But this might be a little bit difficult because you are using a non-core page builder, Elementor, which is off-topic in this community and depending on the template you using, make most of WordPress core hooks unavailable. In this case recommend you take a look at Using Ajax articles on WordPress official documentation and then find in the page builder documentation how to handle Ajax and add your own scripts to it.
-
Thanks, I ended up using different menues for logged in users as it makes more sense to me. I used this snippet: // Conditional Nav Menu function wpc_wp_nav_menu_args( $args = '' ) { if( is_user_logged_in() ) { $args['menu'] = 'logged-in'; } else { $args['menu'] = 'logged-out'; } return $args; } add_filter( 'wp_nav_menu_args', 'wpc_wp_nav_menu_args' ); Jan 9, 2021 at 20:10
-
Ok, glad I helped anyway. As you are using different menu everywhere, you might see a decrease in caching and increasing in resourcing usage, though. Jan 9, 2021 at 21:12
Probably somethin like this, but I don't know how to adapt it top my case:
add_action( 'template_redirect', 'dashboard_redirect' ); function dashboard_redirect() { if( is_page( home_url( '/min-konto/' ) ) && ! is_user_logged_in() ) { wp_redirect( home_url( '/dashboard/' ) ); exit(); } }
-
1Please update your question - using the edit button - rather than add each update as an answer.– Q StudioJan 9, 2021 at 16:34