Make WordPress Core

Opened 8 years ago

Last modified 6 days ago

#35124 accepted enhancement

Add get_user() function that maps to get_user_by()

Reported by: sc0ttkclark's profile sc0ttkclark Owned by: joedolson's profile joedolson
Milestone: 6.6 Priority: normal
Severity: normal Version:
Component: Users Keywords: good-first-bug has-patch dev-feedback
Focuses: Cc:


 * Retrieve user info by user ID.
 * @since 4.5
 * @param int $user_id User ID
 * @return WP_User|false WP_User object on success, false on failure.
function get_user( $user_id ) {
        return get_user_by( 'id', $user_id );

Works like get_userdata but meets the standards of get_{object_type} we've put forth in get_post( $id ) / get_term( $id ) / get_comment( $id ) to make it easier for new developers coming into WordPress.

I imagine at some point we could deprecate get_userdata calls, but not sure that should be done in the same release this is introduced.

Attachments (2)

35124.diff (39.1 KB) - added by kushang78 4 months ago.
Added patch for this ticket.
35124.13.02.2024.diff (718 bytes) - added by kushang78 3 months ago.
Added patch for this ticket.

Download all attachments as: .zip

Change History (12)

#1 @SergeyBiryukov
8 years ago

Previously suggested in comment:24:ticket:21120.

#2 @joedolson
7 months ago

  • Milestone set to 6.5
  • Owner set to joedolson
  • Status changed from new to accepted

This really makes sense; it may be a fairly minor change, but it enhances the understandability of the code base.

This ticket was mentioned in Slack in #core by rajinsharwar. View the logs.

4 months ago

#4 @rajinsharwar
4 months ago

  • Keywords good-first-bug added

4 months ago

Added patch for this ticket.

#5 @kushang78
4 months ago

  • Keywords has-patch added

I’ve added the latest patch.

Patch includes below changes:

  • Added get_user() - that maps to get_user_by() in the replace of get_userdata().
  • There are plenty of files that contains get_userdata() function. Successfully Replaced with New get_user() function.
  • The root file of this function - src/wp-includes/pluggable.php
Last edited 4 months ago by kushang78 (previous) (diff)

#6 @sc0ttkclark
4 months ago

Renaming the usage everywhere seems ok to me but not required IMO.

The renaming the function itself should be changed, keep the old function in place but have it call get_user() and then bring the logic into the new function with new phpdoc @since for 6.5.

This ticket was mentioned in Slack in #core by audrasjb. View the logs.

3 months ago

#8 @joedolson
3 months ago

  • Milestone changed from 6.5 to 6.6

We can't just remove a function that simply; get_userdata is extremely widely used, and this change would create a lot of breakage. We can alias it, but removing it is not so ideal.

The big question here is whether we might want to deprecate get_userdata when we add get_user. It's a simpler construction, more predictable, matches more other existing function naming patterns.

I'm not sure this will be ready for 6.5; I haven't had time for it, but I'm going to re-milestone to 6.6.

3 months ago

Added patch for this ticket.

#9 @kushang78
3 months ago

@sc0ttkclark ,

As per your feedback, I’ve made the changes accordingly in the attached latest patch.

The changes:

  • Pointing the new get_user function under get_userdata without touching the entire core functions.

@sc0ttkclark @joedolson,

Can you please check my latest patch and drop your feedback on it.

Last edited 3 months ago by kushang78 (previous) (diff)

#10 @oglekler
6 days ago

  • Keywords dev-feedback added

I believe we need a decision if we are doing this or not, and do whatever will be decided. We have like 2 weeks until Beta 1, so, this is the time. New function is an enhancement.

Note: See TracTickets for help on using tickets.