• Hi,

    I’m a creating a website for my mother, using WordPress. As a writer, she needed a place to host blog articles, a little shop for freelance writing and a user administration to offer courses.

    I have created the user roles using WP-Members, the course using LearnPress and created the pages.

    Only problem is that I want a simple button on the main page, “Login/Register” that transforms to “My profile” once logged in.
    I searched a lot on internet but couldn’t find that particular feature.

    Thanks in advance for the replies !

Viewing 9 replies - 1 through 9 (of 9 total)
  • Hi, @cdltsupport

    The simplest way to add a login link is by adding the meta widget to your sidebar.

    Go to Appearance ? Widgets page and add the Meta widget to a sidebar.

    Hope this helps.

    Thread Starter cdltsupport

    (@cdltsupport)

    Hi Vlad,

    Thank you for your quick reply !

    It does seem to work indeed, but do you know if it’s customizable ?

    Kind regards,
    Thomas

    No, it’s not customizable.

    Are you comfortable adding a few lines of code in your functions.php theme file or should I look up a plugin instead? I haven’t found yet a plugin to reccomend, but I can keep looking.

    Thread Starter cdltsupport

    (@cdltsupport)

    Hi again,

    It’s sad haha

    If it’s only removing some lines, I can do it, but I’m far from a developer sadly. I did look out for buttons like this, but there is always a “bad” side on it.

    Okay, I’ll come back in a bit with some lines of code to create a custom [shortcode] to put in your sidebar/widget that will be fully customizable.

    So, in your functions.php file add at the end of the file the following piece of code:

    function custom_links_shortcode( $atts ) {
    	$atts = shortcode_atts(array('login_txt' => 'Login','profile_txt' => 'My profile',),$atts);
    	if(is_user_logged_in()) echo '<a href="'.get_edit_profile_url().'">'.$atts['profile_txt'].'</a>';
    	else echo '<a href="'.wp_login_url().'">'.$atts['login_txt'].'</a>';
    }
    add_shortcode( 'custom_links', 'custom_links_shortcode' );

    What it does?

    • It adds a custom shortcode [custom_links]
    • If the user is logged in it displays a link to the user profile
    • If the user is not logged in it displays a link to the login page
    • It gives the user the option to customize the links text

    How to use:

    After adding that code, go to Appearance ? Widgets page and add the Text widget to the sidebar. Give it a title as you wish and in the content area put the following shortcode:

    [custom_links login_txt=" Login text " profile_txt=" Profile text "]

    You can customize what is inside the " " as you wish.

    Hope this helps. Let me know.

    PS: you can also use that shortcode [custom_links] in pages and posts if you wish.

    Thread Starter cdltsupport

    (@cdltsupport)

    It seems beautiful, thank you very much !

    How can I customize the redirections for those 2 functions ?

    If you want custom redirections, the code needs to be changed as it follows:

    function custom_links_shortcode( $atts ) {
    	$atts = shortcode_atts(array('login_txt' => 'Login','profile_txt' => 'My profile','redirect' => '',),$atts);
    	if(is_user_logged_in()) { $gotolink= ($atts['redirect']!='') ? $atts['redirect']  : get_edit_profile_url(); echo '<a href="'.$gotolink.'">'.$atts['profile_txt'].'</a>';}
    	else echo '<a href="'.wp_login_url($atts['redirect']).'">'.$atts['login_txt'].'</a>';
    }
    add_shortcode( 'custom_links', 'custom_links_shortcode' );

    How it works?

    If the user is logged in and there is a redirect="some link" in the shortcode, it will link there. If redirect is not defined, it will go to the user’s profile.

    If the user is not logged in and there is a redirect="some link" in the shortcode, it will go there after logging in. If redirect is not defined, it will go to main dashboard after logging in.

    The shortcode also needs to be updated:
    [custom_links login_txt="Login text" profile_txt="Profile text" redirect="yourRedirectURLhere"]

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Login Button’ is closed to new replies.