How to create a WordPress function with wp_get_current_user()

Hello. I am currently working on a local test project that includes a custom toolbar at the top of the page. This toolbar shows different content depending on if the user is logged-in or out. Currently, I have this toolbar working perfectly, but all the code that I've used to create it is in a header.php file. Ideally, I would like to move this information into a custom WordPress plugin so that it is portable and uses shortcodes to display content.

I have checked the WordPress Codex and Developer information, have taken some online courses on creating plugins, and Googled to find as much information as I can to accomplish my goal.

I am fine with writing the vast majority of the code needed to create this plugin, but can you help me understand how to access WP_Userdata in a plugin using wp_get_current_user(), and in a way that will be secure?

Here is what I have that is working in the header.php file:

<?php
  // Current User Information
  $current_user = wp_get_current_user();

  $current_user_display_name = $current_user->display_name;
  $current_user_id = $current_user->ID;

  /**
   * example Safe usage: $current_user = wp_get_current_user();
   * if ( !($current_user instanceof WP_User) )
   *     return;
   */
  ?>

  <div id="toolbar-wrapper">
    <div id="toolbar-container" style="top: 0px;">
      <ul class="col-sm-6 col-md-6 col-lg-5 nav navbar-nav navbar-right user-topbar">
        <li class="dropdown pull-right">
          <span class="welcome-text">Welcome, </span>
          <a href="#" class="dropdown-toggle welcome-dropdown" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false"><?php echo $current_user_display_name; ?><span class="caret"></span></a>
          <ul class="dropdown-menu">
            <li></li>
            <li></li>
            <li></li>
            <li role="separator" class="divider"></li>
            <li></li>
          </ul>
        </li>
      </ul>
    </div>
  </div>

If I could just get help on the right track that would be great! Thank you!

  • Vaughan

    Hi chadwick,

    Hope you're well?

    You would access it inexactly the same way pretty much.

    Though, if you just want to display content to logged in users, you don't actually need WP_user

    you can simply use;

    if(is_user_logged_in()) {
        /* your logged in code here */
    } else {
        /* your logged out code here */
    }

    What particular data are you looking for?

    get_current_user() will grab the wp_user object and allow you to access anydata in the wp_users table by it's column name

    So if you want the nickname

    $current_user = get_current_user();
    echo $current_user->nickname;

    But if you want to put that in a plugin and have it automatically inject it into the header itself, you would need to use an action to add it to wp_head

    add_action('wp_head', 'my_custom_head_function');
    function my_custom_head_function() {
      // Current User Information
      $current_user = wp_get_current_user();
    
      $current_user_display_name = $current_user->display_name;
      $current_user_id = $current_user->ID;
    
      /**
       * example Safe usage: $current_user = wp_get_current_user();
       * if ( !($current_user instanceof WP_User) )
       *     return;
       */
      ?>
    
      <div id="toolbar-wrapper">
        <div id="toolbar-container" style="top: 0px;">
          <ul class="col-sm-6 col-md-6 col-lg-5 nav navbar-nav navbar-right user-topbar">
            <li class="dropdown pull-right">
              <span class="welcome-text">Welcome, </span>
              <a href="#" class="dropdown-toggle welcome-dropdown" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false"><?php echo $current_user_display_name; ?><span class="caret"></span></a>
              <ul class="dropdown-menu">
                <li></li>
                <li></li>
                <li></li>
                <li role="separator" class="divider"></li>
                <li></li>
              </ul>
            </li>
          </ul>
        </div>
      </div>
    <?php
    }

    Then place that in your plugin or even in your theme functions.php or a mu-plugin

    The above code will then automatically be added to the header of the site.

    Hope this helps

  • Chadwick

    Vaughan

    Yes, I am well. Thanks for asking! And you?

    Thank you so much for your help!

    What particular data are you looking for?

    To answer your question, there are two states for the toolbar. The logged-out state shows a call-to-action message. While the logged-in state will incorporate specific user information like their avatar, name, and user-related links for the site, once I build it out a little more.

    Also, rather than just adding it straight to wp_head, I need the toolbar to show up in a specific spot within the design of my site. I was thinking about generating shortcodes and going that route, but if you know a better, more efficient way, that would be awesome!

    Thanks again!

  • Vaughan

    Hi,

    Ok, I see, a shortcode would certainly give you more flexibility for sure.

    The following guides should hopefully get you going on creating the shortcode.

    https://codex.wordpress.org/Shortcode_API

    https://www.elegantthemes.com/blog/tips-tricks/how-to-create-shortcodes-in-wordpress

    there's a generator here too, though I haven't used it personally.

    https://generatewp.com/shortcodes/

    with regards to Avatar, there's a specific function for getting avatars.

    https://codex.wordpress.org/Function_Reference/get_avatar

    Or if you want the avatar URL, you can use:

    https://developer.wordpress.org/reference/functions/get_avatar_url/

    I don't know if this will be any good to you or not, but there's aplugin which adds a lot of useful shortcodes to wp that you can use.

    https://en-gb.wordpress.org/plugins/user-shortcodes-plus/

    And another plugin which I use on my site as it allows you to place shortcodes in menu items which makes it easyto create a menu item that contains a dynamic username for example such as with BP installed.

    http://wordpress.org/plugins/shortcode-in-menus/

    Hope this helps

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.