Support » Developing with WordPress » How can I use current user id in html code in any post?

  • Hello,

    I would like to get current users id and use it in the html code of certain posts & pages.

    I tried including <?php echo get_current_user_id(); ?> in the code but I don’t think it worked.

    I tried to print the current user id as text to check if its working.

    Any suggestions?

    Thank you

Viewing 7 replies - 1 through 7 (of 7 total)
  • Moderator Steve Stern (sterndata)

    (@sterndata)

    Forum Moderator & Support Team Volunteer

    Are you looking for a shortcode or something that goes in a page template? Ultimately, what do you want to do with the user ID?

    Thread Starter overdriver

    (@overdriver)

    Hello,

    Thank you so much for your answer.

    I’m not looking for a short code.

    I want to get the current user id and add that current user id variable to an embed html code.

    Like

    <a href="http://www.whatever.com/?$user_id?">Click Here</a>

    But I would like to be able to use this in a html code

    • This reply was modified 8 months, 2 weeks ago by overdriver.
    • This reply was modified 8 months, 2 weeks ago by overdriver.
    • This reply was modified 8 months, 2 weeks ago by overdriver.
    Moderator Steve Stern (sterndata)

    (@sterndata)

    Forum Moderator & Support Team Volunteer

    The basic code to get the variable would be *something like*:

    $uobj = wp_get_current_user();
     if ($uobj->ID == 0 ) { // there is no current user
         // do something here?
        } else {
     echo '<a href="http://www.whatever.com/' . esc_html( $uobj->ID ) . "'>Click here< /a>.';
     }

    see https://developer.wordpress.org/reference/functions/wp_get_current_user/

    What do you mean by “I would like to be able to use this in a html code”?

    Thread Starter overdriver

    (@overdriver)

    Thank you so much for the answer!

    I am embedding webpages in my posts. I need to add current users id in the embedded link so that webpage knows how many individual users from my website is seeing their embedded pages.

    So I want to add the “current user id” variable to the end of the embed code.

    Their embedded url should be like:

    https://www.whatever.com/…/embed/?u=USER_ID_IN_MY_SYSTEM

    I am trying to insert current user id in that embedded link in my posts.

    So I am not adding this variable in any of the php files but I need to add the variable directly in each post I create in WordPress

    • This reply was modified 8 months, 2 weeks ago by overdriver.
    • This reply was modified 8 months, 2 weeks ago by overdriver.
    Moderator Steve Stern (sterndata)

    (@sterndata)

    Forum Moderator & Support Team Volunteer

    I’d do that as a shortcode, so my posts would have something like

    [my_embed url="http://example.com"]

    which would produce the link you want.

    If it goes on EVERY post, then it might be better to do it as a filter on the_content(), as that makes it very easy to put it at the bottom of a post.

    Thread Starter overdriver

    (@overdriver)

    Thank you so much!

    Creating a shortcode is a great idea. I’ll do a research on how I can do that

    Moderator Steve Stern (sterndata)

    (@sterndata)

    Forum Moderator & Support Team Volunteer

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘How can I use current user id in html code in any post?’ is closed to new replies.