• Skip to main content
  • Skip to forum navigation

StudioPress

  • Shop for Themes
  • My StudioPress

Forum navigation

  • Home
  • General Genesis Discussions
  • StudioPress Themes
  • Genesis Blocks
    • Genesis Blocks
    • Genesis Custom Blocks
  • Retired Themes
  • FAQs
  • Forum Rules
  • Internationalization and Translations
  • Forum Bugs and Suggestions
  • Forum Log In

Are You Using The WordPress Block Editor?

Genesis now offers plugins that help you build better sites faster with the WordPress block editor (Gutenberg). Try the feature-rich free versions of each plugin for yourself!

Genesis Blocks Genesis Custom Blocks

Make a shortcode to display username

Welcome!

These forums are for general discussion on WordPress and Genesis. Official support for StudioPress themes is offered exclusively at My StudioPress. Responses in this forum are not guaranteed. Please note that this forum will require a new username, separate from the one used for My.StudioPress.

Log In
Register Lost Password

Community Forums › Forums › Archived Forums › General Discussion › Make a shortcode to display username

This topic is: not resolved
  • This topic has 4 replies, 2 voices, and was last updated 13 years, 1 month ago by gomlers.
Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • March 31, 2013 at 4:08 pm #32358
    gomlers
    Member

    I would like to echo the members username, once she logs into my site.

    I've tried "everything", but I cant get it right.

    Anyone who knows what to write in the functions-file, so I can output the members username by putting [username] in a post or page?

    I've tried something similar to this:

    add_shortcode( 'username', output_username' );
    function output_username( ) {
    return $current_user->user_login;
    }

    I am a complete beginner to php, so please don't laugh to hard... :\ i KNOW it may seems silly what I've written...

    March 31, 2013 at 9:34 pm #32382
    David Chu
    Participant

    Hi,
    You're very close - it's not silly, if you keep going you'll be great. Many people won't even try PHP, so I applaud you! I changed your parentheses to a different style. (yours look like Mac quotes, not sure if they will work, they might!). I added one missing paren, and the key thing is making $current_user a global variable so it can be "found".

    add_shortcode( 'username', 'output_username' );
    function output_username() {
       global $current_user;
       return $current_user->user_login;
    }
    

    Dave


    Dave Chu ยท Custom WordPress Developer – likes collaborating with Designers

    April 1, 2013 at 3:16 pm #32520
    gomlers
    Member

    Wow ๐Ÿ™‚ Very nice, it actually worked ๐Ÿ˜€

    Thank you, and thanks for beeing nice!

    Can I ask why I need to put the $current_user as a global?

    Is it because it need to "found" outside of my function. So if a variabel is used only to hold information within a function, it doesn't need the global?

    April 1, 2013 at 4:02 pm #32530
    David Chu
    Participant

    You're welcome, and glad to help! I like to encourage someone with initiative. ๐Ÿ™‚ And besides, by tweaking the function above and testing it, I now have an extra nice block of code I can use sometime!

    Yes, in our custom functions, generally a variable is "local" - only usable in that function. And some variables are defined "global" elsewhere, so your function can access them. And some are limited in scope, like $current_user, so they must be defined global to be available in your function. You can test this pretty quickly, by maybe trying to echo the variable, or you'll just notice that you're getting nothing.

    I've also noticed that sometimes the order of your functions may affect whether you can "get at" the variable. Some things, for instance, are only accessible in the loop, for instance.

    When I was learning Genesis (and just WordPress in general), I found that $post sometimes needs to be defined globally in a function, too. A super hard-core PHP programmer would probably have more to say about all that, and might have other details.

    Dave


    Dave Chu ยท Custom WordPress Developer – likes collaborating with Designers

    April 1, 2013 at 7:48 pm #32554
    gomlers
    Member

    Thank you ๐Ÿ™‚ Learning by doing is the best way - although help from others are necessary sometimes.

    This helped a lot!

  • Author
    Posts
Viewing 5 posts - 1 through 5 (of 5 total)
  • The forum ‘General Discussion’ is closed to new topics and replies.

CTA

Ready to get started? Create a site or shop for themes.

Create a site with WP EngineShop for Themes

Footer

StudioPress

© 2026 WPEngine, Inc.

Products
  • Create a Site with WP Engine
  • Shop for Themes
  • Theme Features
  • Get Started
  • Showcase
Company
  • Brand Assets
  • Terms of Service
  • Accptable Usse Policy
  • Privacy Policy
  • Refund Policy
  • Contact Us
Community
  • Find Developers
  • Forums
  • Facebook Group
  • #GenesisWP
  • Showcase
Resources
  • StudioPress Blog
  • Help & Documentation
  • FAQs
  • Code Snippets
  • Affiliates
Connect
  • StudioPress Live
  • StudioPress FM
  • Facebook
  • Twitter
  • Dribbble