Make a shortcode to display username

Community Forums Forums General Discussion Make a shortcode to display username

This topic is: not resolved

This topic contains 4 replies, has 2 voices, and was last updated by  gomlers 1 year, 5 months ago.

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #32358

    gomlers
    Participant
    Post count: 29

    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…

    #32382

    David Chu
    Participant
    Post count: 1355

    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”.

    [php]
    add_shortcode( ‘username’, ‘output_username’ );
    function output_username() {
    global $current_user;
    return $current_user->user_login;
    }
    [/php]

    Dave


    Dave Chu · Custom WordPress Developer – likes collaborating with Designers

    #32520

    gomlers
    Participant
    Post count: 29

    Wow :) Very nice, it actually worked :D

    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?

    #32530

    David Chu
    Participant
    Post count: 1355

    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

    #32554

    gomlers
    Participant
    Post count: 29

    Thank you :) Learning by doing is the best way – although help from others are necessary sometimes.

    This helped a lot!

Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.