WordPress.org

Make WordPress Core

Opened 3 years ago

Closed 2 years ago

#31549 closed enhancement (fixed)

filter meta array in wp_insert_user

Reported by: tharsheblows Owned by: wonderboymusic
Milestone: 4.4 Priority: normal
Severity: normal Version:
Component: Users Keywords: has-patch commit
Focuses: Cc:

Description

Could a filter be added to the $meta array before inserting it into usermeta?

I don't use some of the meta keys and looked and saw they're safe to delete, but realised I can't actually stop them from being set. The reason I'm asking is that count_users is getting very slow for me (I have 110k users), so I'm coming at that from a couple of different directions.

Thanks.

Attachments (6)

31549.diff (424 bytes) - added by tharsheblows 3 years ago.
add filter to meta array in wp_insert_user
31549.1.diff (1.7 KB) - added by tharsheblows 3 years ago.
add in hooks docs
31549.2.patch (1.7 KB) - added by chriscct7 2 years ago.
version number bumps. Array sanity check before for loop
31549.3.patch (1.7 KB) - added by chriscct7 2 years ago.
formatting
31549.patch (1.7 KB) - added by DrewAPicture 2 years ago.
Fixed docs
31549.5.diff (634 bytes) - added by tharsheblows 2 years ago.
add in $update to see if user is being created or updated

Download all attachments as: .zip

Change History (19)

@tharsheblows
3 years ago

add filter to meta array in wp_insert_user

#1 @tharsheblows
3 years ago

  • Keywords has-patch added

#2 @DrewAPicture
3 years ago

Hi @tharsheblows, welcome to Trac!

The new filter you're suggesting will need hook docs to go along with it.

#3 @tharsheblows
3 years ago

Oops. I've added them in - let me know any typos / misunderstandings / mistakes. Thanks! :)

@tharsheblows
3 years ago

add in hooks docs

#4 @chriscct7
2 years ago

  • Keywords needs-refresh added; has-patch removed
  • Milestone changed from Awaiting Review to 4.4
  • Owner set to chriscct7
  • Status changed from new to assigned

This patch needs a refresh for the version in the doc hook and some sanity checks. Will get a revised patch up and going when I get a chance

@chriscct7
2 years ago

version number bumps. Array sanity check before for loop

@chriscct7
2 years ago

formatting

#5 @chriscct7
2 years ago

  • Keywords has-patch commit added; needs-refresh removed
  • Status changed from assigned to accepted
  • Version 4.2 deleted

Looks good to go.

#6 @wonderboymusic
2 years ago

  • Owner changed from chriscct7 to DrewAPicture
  • Status changed from accepted to assigned

#7 @wonderboymusic
2 years ago

  • Status changed from assigned to reviewing

@DrewAPicture
2 years ago

Fixed docs

#8 @DrewAPicture
2 years ago

  • Owner changed from DrewAPicture to wonderboymusic

31549.patch fixes some of the types and formatting in the hook docs. Otherwise look good to go.

#9 @wonderboymusic
2 years ago

I'm not sure I would cast to array - maybe check after for ! $meta and then set to array() - I'll think about it

php > $a = null;
php > $b = (array) $a;
php > var_dump( $b );
array(0) {
}
php > $a = false;
php > $b = (array) $a;
php > var_dump( $b );
array(1) {
  [0]=>
  bool(false)
}
php > 

#10 @wonderboymusic
2 years ago

  • Resolution set to fixed
  • Status changed from reviewing to closed

In 33708:

In wp_insert_user(), add a filter: insert_user_meta, to filter a user's meta values and keys before the user is created or updated.

Props tharsheblows, chriscct7, DrewAPicture.
Fixes #31549.

#11 @geminorum
2 years ago

  • Resolution fixed deleted
  • Status changed from closed to reopened

Since it's the same for inserting new user and updating one, there's no safe way to see if the empty meta such as description is actually initiated empty or must be deleted on this update.

thinking the whole point of the filter is to bypass saving empty meta upon registration.

suggesting pass full info to the filter, i.e. $update

Last edited 2 years ago by geminorum (previous) (diff)

#12 @tharsheblows
2 years ago

That makes more sense! I've added it in.

@tharsheblows
2 years ago

add in $update to see if user is being created or updated

#13 @boonebgorges
2 years ago

  • Resolution set to fixed
  • Status changed from reopened to closed

In 34266:

Tell the insert_user_meta filter whether user is being updated.

insert_user_meta was introduced in [33708]. This changeset passes the
$update parameter to it.

Props tharsheblows, geminorum.
Fixes #31549.

Note: See TracTickets for help on using tickets.