WordPress.org

Ready to get started?Download WordPress

Forums

can't get $user_ID in plugin (8 posts)

  1. manstraw
    Member
    Posted 8 years ago #

    I'm writing a plugin that creates a new sub menu in users. it's called with ...wp-admin/profile.php?page=headshot I everything except one tiny thing that is confusing the heck out of me. I can't get the current user id. I need it so I can name a file with that number.

    For testing the plugin, I have some extremely simple code as follows:

    echo $user_ID;

    It return nothing. So I thought it might need:

    global $user_ID;
    echo $user_ID;

    I'm new to wordpress, but from what I've read, $user_ID is a global variable. Am I missing something really simple here? Or is there some reason that variable is not available through profile.php (which makes no sense to me).

    Help is appreciated.

  2. davidchait
    Member
    Posted 8 years ago #

    well, user_ID and other user info is only available after pluggable-functions loads, in case plugins want to override things. so you should hook to not start any 'main work' of your plugin until after that...

    usually good places would be to hook on the action 'plugins_loaded', or 'init'. But you might hook on the header or footer, or in your case an admin-specific hook might be better.

    Then $user_ID should be available (since security auth has to have occurred if you are in the admin pages!).

    -d

  3. manstraw
    Member
    Posted 8 years ago #

    Thanks for that info. I was actually beginning to suspect that. I came up with an insecure work around by passing from a http post, as $user_ID was available in the hook where I had my html form. (The receiving code from the http post is unhooked, as it does stuff that is ultimately independent from wordpress. It just stores a file, but wants the user_ID to create the filename.)

    I'm not sure how to put the code in a hook in this case. I'm thinking it would be a filter hook as opposed to an action hook? Well, like I said, I'm just figuring this wordpress out for the first time. Once I figure this out, I'll clean up my code and post the plugin.

  4. davidchait
    Member
    Posted 8 years ago #

    No, actually both cases I gave above are action hooks, not filter hooks. Very easy to do off either one.

    -d

  5. manstraw
    Member
    Posted 8 years ago #

    ok, I threw my tasks into a function, and have it called with an admin_head action. I had to global http_post_files which I use, but not _post, which I also use. Anyway, I now get $user_ID. I called it with the admin_head since later on the page, I actually display the image the script just uploaded. Using admin_head makes sure it's been uploaded before the html is generated. (this probably doesn't matter, but in case we have fast connections, well, I'm being anal :) ). Anyway, everything works. Thanks for you guidance. Time to clean up some code and maybe publish it.

    btw, it's called headshot, and is a simple single image handler for author's mug shots. (maybe a better name is mugshot, hmmm...) 'Authors can easily upload their own headshot. Two sizes are accessible by passing icon or large to the function. put <?php headshot(); ?> in the loop, and it generates the html to display the image. That's the short description. It has some other frills too. I created this because I'm building a site that will have many many authors, and i needed a way for them to upload their own image in a managed way. I couldn't find anything that did this as I wanted.

  6. davidchait
    Member
    Posted 8 years ago #

    neat, personalized gravatars/avatars. there's some stuff like that out there, but probably not anywhere matching exactly what you described (i.e., multi-author, simple upload, etc.).

    Definitely let peeps known when you've got it working.

  7. kendallinternet
    Member
    Posted 8 years ago #

    manstraw, the plug-in you describe is exactly what I'm looking for - to provide each registered user the ability to upload their own image to their profile. I've searched the web for "headshot plugin" etc. but not found your plugin or web site etc!

    I did find your comment on Andrew Ferguson's post about modifying the user menu (http://www.andrewferguson.net/2006/05/16/add-data-to-user-field-in-wordpress/), and others like this thread on the WordPress site itself.

    Do you still use/have the "headshot" plugin code or if not do you know where I can find a plugin to do the same thing?

    Any help you could provide would be much appreciated.

  8. manstraw
    Member
    Posted 8 years ago #

    Hi,

    I never made it public, and I haven't tested it with 2+ yet. But I can probably be convinced to look it over, clean it up where necessary, and publish it.

    I know there are a few things missing, like proper checks for size. I think I only check the height right now. And I think I only support jpeg, which is probably good enough for many folks.

    I'm dealing with a minor server crisis, and have a couple of other priorities to take care of first, and then I'll look at this. If I decide I don't have time to clean it up, I may elect to give it to just you to test privately. I wrote it so long ago, I'm not quite sure what messy bunch of code I might find when I crack it open! :)

Topic Closed

This topic has been closed to new replies.

About this Topic