Community Forums › Forums › Archived Forums › General Discussion › Make a shortcode to display username
- This topic has 4 replies, 2 voices, and was last updated 11 years ago by gomlers.
-
AuthorPosts
-
March 31, 2013 at 4:08 pm #32358gomlersMember
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 #32382David ChuParticipantHi,
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 #32520gomlersMemberWow ๐ 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 #32530David ChuParticipantYou'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 #32554gomlersMemberThank you ๐ Learning by doing is the best way - although help from others are necessary sometimes.
This helped a lot!
-
AuthorPosts
- The forum ‘General Discussion’ is closed to new topics and replies.