Quick Tip: Programming User Management in WordPress

Quick Tip: Programming User Management in WordPress

If you’re programming user management in WordPress there are a number of functions we have the convenience of using on the front-end. If you want to use the same functions in the back-end, though, you’ll need to include some “dependencies.”

Programming User Management in WordPress

For example, let’s say you want to use something like:

You’ll find then incorporating them onto the front-end of the site is easy. But if you want to use them in a plugin in, say, something like this:

wp_create_user(     'acme-demo-user',     wp_generate_password(12, true, false),     'acme-demo-user@acme.com' );

Of if you want to delete a user with a given email:

wp_delete_user(     (get_user_by('email', 'acme-demo-user@acme.com'))->ID );

Then you’ll get an error in WordPress. It’s a simple fix, though. At the top of your file – regardless of if its procedural code or a class – add the following lines:

require_once(ABSPATH . 'wp-admin/includes/user.php'); require_once(ABSPATH . 'wp-includes/pluggable.php');

This will give you access to everything you need for the above functions.


Note: If you’re only going to use one or some of the above functions, you may not need both of those files. Refer to the linked Developer Resources for more information.

Advertisement

About The Author

Tom

Archives