How do send membership status (ie. %username% from membership plugin into the membership text/login

My ask, When a member logs into site, send a "you are logged in as %username%" either into the membership text/login widget or send it to the header or menu bar. Additionally, send membership level "free , premium, etc."-

Thanks

Mike

  • aristath

    Hello again Mike,
    I haven't tested this but you could try adding something like this in your template files whereever you want it:

    <?php
      $current_user = wp_get_current_user();
      if ( !($current_user instanceof WP_User) ) :
        return;
      endif;
    
      echo 'You are logged in as ' . $current_user->user_login . '<br />';
      if ( current_user_on_level( 1 ) :
        $levelname = 'level 1';
      elseif ( current_user_on_level( 2 ) ) :
        $levelname = 'level 2';
      elseif ( current_user_on_level( 3 ) ) :
        $levelname = 'level 3';
      endif;
    
      if ( current_user_is_member() ) :
        echo 'Membership Level: ' . $levelname;
      endif;
    
    ?>

    Of course you'll have to customize that with your own levelnames etc, but the principal will still be the same.

    I hope that helps!

    Cheers,
    Ari.

  • Mike

    @Ari-thanks for your suggestion. Here is what I ended up putting together at the top of functions.php

    function my_wp_nav_menu_args( $args = '' ) {
    
    if( is_user_logged_in() ) {
    	$args['menu'] = 'logged-in';
    	add_filter('wp_nav_menu_items', 'add_login_logout_link', 10, 2);
    	function add_login_logout_link($items, $args) {
    
    $current_user = wp_get_current_user();
      if ( !($current_user instanceof WP_User) ) :
        return;
      endif;
    	    <strong><div class="log_name_comment"></strong>
    	      echo 'You are logged in as ' . $current_user->user_login . '<br />';	
    
    	        ob_start();
    	        wp_loginout('index.php');
    	        $loginoutlink = ob_get_contents();
    	        ob_end_clean();
    	        $items .= '<li>'. $loginoutlink .'</li>';
    	    return $items;

    I am still learning on the div class items as I would need to assign it to a class so I can than manipulate it via css, correct ?
    change font color, location, etc.

    any help would be appreciated.

    Mike

  • Mike

    @Ari- sorry for the delayed response back, nice long w/e away.
    The above code errors out when I try to implement it inside the function.
    <strong><div class=&quot;log_name_comment&quot;></strong>

    I cannot add the class in order to manipulate it in CSS as it gives an unexpected < sign error.

    I simply want the 'you are logged in as' float:right near my social icons.

    thanks for any help. See picture attached.

    Mike

  • Ash

    Hello Mike

    I just saw your code and it seems to me that you want to add a login/logout link in the menu, also you want to show a text "You are currently logged in as USERNAME".

    My suggestion will be:

    1. For adding login/logout link in menu add this in functions.php:

    function add_login_logout_link($items, $args)
    {
        if(is_user_logged_in())
            $items .= '<li><a href="' . wp_logout_url() .'">Logout</a></li>';
        else
    	$items .= '<li><a href="' . wp_login_url() .'">Login</a></li>';
        return $items;
    }
    add_filter('wp_nav_menu_items', 'add_login_logout_link', 10, 2);

    2. To show that text, add this in funcstions.php:

    function show_current_user() {
    	if(is_user_logged_in()) {
    		$current_user = wp_get_current_user();
    		echo "<span class='mem-info'>You are currently logged in as ". $current_user->user_login ."</span>";
    	}
    }

    And call the function show_current_user() in the template where you need it. Now if you need styling this text, you can do so in your css with the class name mem-info which is added in the code.

    Hope it helps! Please feel free to ask more question if you have.

    Cheers
    Ash

  • Mike

    Ash-thanks for the code, I made the following work and have provided it below in the event someone else wants to use it..

    Note- you need to create an additional menu, code below calls for a menu called 'logged-in' & 'home' (my default).
    I further customized my logged-in menu to remove add various options and did the same for a "logged out user" which sees the home menu.

    The code will give a 'Logout' entry on your menu with a you are logged in as %username% in the header.
    Additionally,

    Note - you'll need to customize the css style

    #title-area {width : 106% !important} /* for logged in statement in header */
    .mem-info{color:#ffff00;}

    function my_wp_nav_menu_args( $args = '' ) {
    
    if( is_user_logged_in() ) {
    	$args['menu'] = 'logged-in';
    	add_filter('wp_nav_menu_items', 'add_login_logout_link', 10, 2);
    
    		/* $user = get_user_by( 'email', 'user@example.com' );
    		$user_info = get_userdata(1);
          $user_email = $user_info->user_email;*/
          $current_user = wp_get_current_user();
    
    		echo "<span class='mem-info'>You're logged in as ". $current_user->user_login ."</span>";
    		/*echo "<span class='mem-email'>Your email address is ". $user->user_email ."</span>";*/
    
    	function add_login_logout_link($items, $args) {
    
    $current_user = wp_get_current_user();
      if ( !($current_user instanceof WP_User) ) :
        return;
      endif;
    
    	        ob_start();
    	        wp_loginout('index.php');
    	        $loginoutlink = ob_get_contents();
    	        ob_end_clean();
    	        $items .= '<li>'. $loginoutlink .'</li>';
    	    return $items;
    
    	}
    
    } else {
    	$args['menu'] = 'home';
    }
    	return $args;
    }
    add_filter( 'wp_nav_menu_args', 'my_wp_nav_menu_args' );

    The real credit for the code and help goes to @PC who helped me put this all together.

    Mike

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.