• Resolved tiagovieirar

    (@tiagovieirar)


    Hi everyone. I will try to explain my issue as best as I can, I am in a bit of a struggle with this:

    So, I have a icon on my header that redirects users to their “my-account” page. The My Account widget that has the dashboard (orders, change adress, info, etc) also has a integrated login and register form. However, I want to use a custom form for loggin in and registering “Login” widget.

    So I have a separate page domain/login. That is the page I want the login to happen and not in the form of the “my account” widget. However, I have no idea how I can make this work. I want the icon on the header to redirect users to the login page when they are not logged in already. However, when the user is logged in, I want the same icon to redirect them to the my account page, where they can edit orders, view info etc.

    I’ve been trying to do this all day, am I missing something?

    The page I need help with: [log in to see the link]

Viewing 7 replies - 1 through 7 (of 7 total)
  • Harsh Gajipara

    (@harshgajipara)

    function header_icon_shortcode() {
        if ( is_user_logged_in() ) {
            $my_account_link = get_permalink( get_option('woocommerce_myaccount_page_id') ); // Adjust this according to your setup
            $icon_link = esc_url( $my_account_link );
            $icon_alt = 'My Account';
        } else {
            $login_link = wp_login_url( get_permalink() );
            $icon_link = esc_url( $login_link );
            $icon_alt = 'Login';
        }
    
        // Replace 'path_to_your_icon_image.png' with your actual icon image path
        $icon_image = '<img src="path_to_your_icon_image.png" alt="' . esc_attr( $icon_alt ) . '">';
        return '<a href="' . $icon_link . '">' . $icon_image . '</a>';
    }
    add_shortcode( 'custom_header_icon', 'header_icon_shortcode' );
    

    Hi there,
    You can add this code to your theme’s functions.php file with neccesary updates mentioned. And replace the icon widget with this shortcode [custom_header_icon].
    Thanks!

    Thread Starter tiagovieirar

    (@tiagovieirar)

    Thank you for your response!
    So I need to replace the “woocommerce_myaccount_page_id” with the ID of the page where I want the login and register to happen? And then the user who is logged in will be automatically redirected to the “my-account” page instead?
    Also, replace “path_to_your_icon_image.png” with the icon url, and also add the short code in replace of the current icon in the header?

    Sorry for all the questions, I am new to wordpress and coding overall!

    Moderator bcworkz

    (@bcworkz)

    get_option('woocommerce_myaccount_page_id') returns the ID of the configured WC account page where logged in users would be taken. I think this is the behavior you want? If so, nothing to change here if that’s a valid option value.

    wp_login_url( get_permalink() ) returns the URL of the current page. Probably not what you want. If you have a custom login page, pass the ID of that page to get_permalink(). This would be the link for non-logged in users.

    If the link+icon is part of the widget area, then you can replace it with the suggested shortcode in an appropriate element. If the link+icon is inserted by other means then replacement depends on how it was initially inserted.

    It looks like the current icon is inline SVG and not an image file. You could either replace it with an actual image file, use the URL for an equivalent external .svg file, or assign the inline SVG to $icon_image instead of the suggested img tag.

    Thread Starter tiagovieirar

    (@tiagovieirar)

    Hi! I tried to put in that code and it works… but only for already logged in users, which are redirected to “my-account” page as they should. However… if I open a privacy tab and open the website, of course this time not logged in, it is not acessible and I get a message “An expected error occured”.

    Thread Starter tiagovieirar

    (@tiagovieirar)

    maybe I am typing the login page on “wp_login_url( get_permalink() )” wrong?

    I am doing it this way: wp_login_url( get_4407() ) – that is the ID of the custom login page

    Thread Starter tiagovieirar

    (@tiagovieirar)

    Sorry for another reply, but something came up: I think I might be looking at this the wrong way.

    Let me explain, what I really need is the user icon on the header to redirect to the page “my-account”.

    However, I want to make that page non accessible to logged out users. Those logout users, when clicking on the user icon, would be redirected to the proper custom login page. Then, when they would log in, they would be redirected to the my-account page. Then, if they chose to log-out, to the /home page. I think the only way to achieve this is really with .php

    Thread Starter tiagovieirar

    (@tiagovieirar)

    Problem solved, I add another snippet to redirect and it works now! Thank you everyone.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Login and My Account’ is closed to new replies.