Current User link shortcode

This function adds a shortcode that display a link if user is logged in.

Make sure to:

  • change link and text on line 11
Shortcode:

[current_user_link]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/*
 * Current User link shortcode
 * Use like so: [current_user_link] This is displayed only if current user is logged in.
 */
 
add_shortcode( 'current_user_link', 'wppbc_current_user_link' );
function wppbc_current_user_link( $atts, $content ) {
   if ( is_user_logged_in() ) {
      $id = get_current_user_id();
      // make sure to change the URL to represent your setup.
      return "<a href='http://website.com/user-listing-page/user/{$id}'>Your User Page</a>";
   }
 
   return ;
}