#59212 closed enhancement (fixed)
Call `add_user_meta()` when adding metadata to a new user
| Reported by: |
|
Owned by: |
|
|---|---|---|---|
| Milestone: | 6.4 | Priority: | normal |
| Severity: | normal | Version: | |
| Component: | Users | Keywords: | has-patch |
| Focuses: | performance | Cc: |
Description (last modified by )
When a user is created via wp_insert_user(), several meta fields are added to the user (including nickname, first name, last name, and various preferences). These are all added using update_user_meta() instead of add_user_meta(). This means an unnecessary database query is performed for each meta field to first check whether a matching meta row exists.
By switching to add_user_meta(), 11 database queries are saved by default when inserting a user.
Todo
- Provide list of before/after queries when inserting a user
- Confirm whether test coverage is sufficient
- Provide steps for testing
- Investigate the same for posts, comments, and terms in follow up tickets
Change History (7)
This ticket was mentioned in PR #5093 on WordPress/wordpress-develop by @johnbillion.
2 years ago
#1
- Keywords has-patch added; needs-patch removed
#3
@
2 years ago
Testing steps:
- Call
wp_insert_user()with at leastuser_loginanduser_emailprovided. - Confirm that all default meta fields are correctly inserted (several have an empty value by default):
nicknamefirst_namelast_namedescriptionrich_editingsyntax_highlightingcomment_shortcutsadmin_coloruse_sslshow_admin_bar_frontlocale
- Repeat with a
meta_inputfield included to include custom user meta fields
The test_wp_insert_user_with_meta() test covers this functionality and is sufficient for confirming that user meta data is saved when a user is created.
Database queries:
Before this change: 54
After this change: 43
As expected, this saves 11 database queries, one for each of the default meta values stored with every user.
#5
@
2 years ago
- Owner set to johnbillion
- Resolution set to fixed
- Status changed from new to closed
In 56478:
This saves an unnecessary database query to look up an existing row that does not exist.
Trac ticket: TBD