I’m writing this post in response to a question that was asked during my WP-CLI Talk at WordCamp Columbus. After demonstrating how you can import users into WordPress from the command line using the:
wp user import-csv
command, one attendee asked if, while looping through the rows in the CSV file, WP-CLI was directly writing the user data to the database, or if it was calling the built-in WordPress functions for doing so, allowing you to hook into WordPress’ actions and filters. It was a good question, one that I wasn’t 100% confident answering in front of the audience.
I’ve now looked into the code, and found….
This command, as it should, uses the WordPress functions wp_insert_user() and wp_update_user() to add users to the database. These functions call the user_register (new user) or the profile_update (updated user) actions upon successful completion. You can also hook into a variety of filters during this process, including:
- pre_user_login
- pre_user_nicename
- pre_user_url
- pre_user_email
- pre_user_nickname
- pre_user_first_name
- pre_user_last_name
- pre_user_display_name
- pre_user_description
Hooking into any of the above listed filters will allow you to modify the value being inserted into those fields before they are written to the database.