Viewing 7 replies - 1 through 7 (of 7 total)
  • 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.