I have tried looking for a simple answer and if there is already a post that I can be sent to, please by all means send me a link.

The background to this may not be needed but I feel that providing it will help to clarify the answer I am looking for.

Coming from Drupal I find WordPress methods both similar and very different, with Drupal I would tell it the name of the page and the callback function to display what I want it to display, I am trying to develop with wordpress but because I am so used to Drupal terminology and methods I am having a difficult time trying to work through sometimes even the simplest of things.

basically what I want to do is to display the result of a function on the users profile page.

the background is this.

A user has a calendar – this is shown month by month, so when a users profile page is opened up the calendar is supposed to show custom post type (an Event) on the date in question.

I have the custom post types created, I have the calendar created, what I need to do is this.

When the page loads I want to grab all the post types of ‘Event’ created by that whomsoever is being viewed. normally I would have grabbed the user by the url, because that is how I would I would know whose page is being viewed. but because I am using wordpress I am given to understand that there are other ways of getting the user data.

The question I suppose is this.

When displaying custom data on a users profile page I need to create an action (or a shortcode) that will inject the calendar into the page, How do I know which profile is being displayed and how can I grab data from a users object (if WP uses a $user object) so that I can pass this as an argument to my code so that I am getting the correct post types?

I also need to clarify this, I am NOT displaying the full custom post types, I just need to get them from the database so that I can then loop through them, discover which of them have a date that matches the month being viewed and display the title of each one.

I can read the data from the database, I can inject this into the calendar, I will be able to (eventually) display this on the profile page (via an action, filter or shortcode) but at the moment I have no idea which user profile is currently being viewed, and how can I access the data of that given user.

Thank you in advance for any help given.

Jim

1 Answer
1

Assuming you are in the back-end (admin), so the user profile page is being rendered by /wp-admin/user-edit.php – there is a global variable that indicates the id of the user whose profile we are on. If you look at the source code for user-edit.php you can see it guarantees (or will die) the existence of the variable $user_id which contains the ID, and will get it from the global object $current_user, if we have a logged-in user.

You could hook this to WP user-edit.php :

function calendar_info( $some_user ) { 
    // we can do something with the $some_user object or we can ignore it...
    global $user_id;   // user id of the profile currently being displayed/edited
       echo get_calendar_data( $user_id );
}
add_action('show_user_profile', 'calendar_info');  // only fires when viewing currently logged-in user's profile page
add_action('edit_user_profile', 'calendar_info');  // only fires when viewing another user's profile page

Those hooks pass in the currently logged-in user object

You need to hook to both of these actions due to the logic of when each of them fires (logged-in user profile versus viewing another user’s profile).

Of course you could have a different function hooked to each action – say for example if you wanted to show something different for a logged-in user viewing their own profile versus viewing another user’s profile.

The only thing not too nice about these hooks is that they fire near the bottom of the profile page, not the top.

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *