Make WordPress Core

Opened 4 years ago

Closed 18 months ago

Last modified 6 months ago

#18709 closed enhancement (fixed)

Hooks in user-new.php

Reported by: standardtoaster Owned by: helen
Milestone: 3.7 Priority: normal
Severity: normal Version: 3.2.1
Component: Administration Keywords: commit has-patch
Focuses: Cc:


It would be nice to be able to enter extra metadata for users when manually creating them from the admin control panel.

I assume it should be as simple as adding a new action, along the lines of the edit_user_profile_update one?

Attachments (6)

new-user-profile.diff (443 bytes) - added by strangerstudios 3 years ago.
Diff to add a "new_user_profile" action to user-new.php
18709.patch (2.4 KB) - added by johnjamesjacoby 23 months ago.
Refresh, use existing naming conventions, and add actions before and after
18709.2.patch (2.6 KB) - added by strangerstudios 20 months ago.
user_new_form_before and user_new_form_after hooks
18709.3.patch (3.4 KB) - added by DrewAPicture 19 months ago.
hooks + passed context + hook docs
18709.4.patch (3.4 KB) - added by DrewAPicture 19 months ago.
18709.diff (2.1 KB) - added by jeremyfelt 18 months ago.

Download all attachments as: .zip

Change History (49)

comment:1 @standardtoaster4 years ago

  • Version changed from 3.3 to 3.2.1

comment:2 @SergeyBiryukov4 years ago

Isn't that already possible by hooking to user_register and checking is_admin() if needed?

comment:3 @standardtoaster4 years ago

It looks like the user_register hook is called AFTER the form is filled out, making it unsuitable for requested user metadata during the account creation process.

I'd like to add fields to the new user form (in the admin interface only). the form appears to be statically defined in user-new.php.

@strangerstudios3 years ago

Diff to add a "new_user_profile" action to user-new.php

comment:4 follow-up: @strangerstudios3 years ago

I think the OP meant to add form fields and other code to the actual form in user-new.php... similar to "edit_user_profile" and "show_user_profile" in user-edit.php.

I'd like to do something similar, and wonder why this hook isn't in place already.

Attached is a patch which just adds a "new_user_profile" action above the roles. (FYI, I created the diff against 3.3-beta2-19067)

A hack to work around not having this hook is to use the "show_password_fields" hook/filter. That hook seems to only be used on the user-new.php and user-edit.php page. It's meant to check if the password field should be shown, but you can output code at that point to add things to the form. You can check FILE or $_SERVERPHP_SELF? to distinguish between the new/edit user pages.

comment:5 in reply to: ↑ 4 @DrewAPicture3 years ago

Replying to strangerstudios:

I think the OP meant to add form fields and other code to the actual form in user-new.php... similar to "edit_user_profile" and "show_user_profile" in user-edit.php.

I'd like to do something similar, and wonder why this hook isn't in place already.

+1 for this. A lot of us have had to work around tacking fields on using the show_password_fields hook before.

comment:6 @aaroncampbell3 years ago

  • Cc aaroncampbell added
  • Keywords dev-feedback added

I just recently had to hack fields into the user-new form using the 'show_password_fields' filter. It's hacky and not an ideal place for the fields. I'd love to see something like this make it in. I'd probably add it to the end of the form, just above the submit button, but where it is in the patch isn't too bad either.

comment:7 @toscho3 years ago

  • Cc info@… added

Currently we have to use JavaScript or output buffering to add custom fields. A real hook would be very useful. The patch looks fine for me.

comment:8 @l3rady3 years ago

  • Cc scott@… added

Please can we have a hook here? WordPress has my my life more difficult due to the lack of hook at that point.

comment:9 @SergeyBiryukov3 years ago

Related/duplicate: #13548

comment:10 @stephenh19883 years ago

  • Cc contact@… added

comment:11 @knutsp2 years ago

  • Cc knut@… added

comment:12 @mtekk2 years ago

  • Cc mtekkmonkey@… added

comment:13 @andyadams2 years ago

  • Cc andyadamscp@… added

comment:14 @RyanGannon2 years ago

  • Cc RyanGannon added

comment:15 @versusbassz2 years ago

  • Cc versusbassz added

comment:16 @mordauk23 months ago

  • Cc pippin@… added

comment:17 @johnjamesjacoby23 months ago

  • Cc johnjamesjacoby added
  • Keywords has-patch added; dev-feedback removed
  • Milestone changed from Awaiting Review to 3.6

Necessary for a bbPress enhancement. #BB2223

Version 0, edited 23 months ago by johnjamesjacoby (next)

comment:18 @nacin23 months ago

This hook is different from edit_user_profile and show_user_profile, as it actually takes place between table rows, rather than between tables.

Ideally, the hook would be placed in such a way that it does not rely on a table being there. One of these days, all of these tables will go away, and a hook like new-user-profile.diff will make that difficult if not impossible.

comment:19 @aaroncampbell23 months ago

I was just talking through this in IRC and was voicing the same concerns. I like the idea of having a hook here, but I'd prefer it be moved outside the table to just before the submit button.

@johnjamesjacoby23 months ago

Refresh, use existing naming conventions, and add actions before and after

comment:20 @johnjamesjacoby23 months ago

18709.patch also adds actions to the network admin page.

comment:21 @sunnyratilal23 months ago

  • Cc sunnyratilal5@… added

comment:22 @JG Visual22 months ago

  • Cc JG Visual added

comment:23 @ocean9022 months ago

  • Milestone changed from 3.6 to Future Release

comment:24 @strangerstudios20 months ago

I like @johnjamesjacoby's patch. Let's get it pushed.

For those wondering how you would process the POST data of any fields added using the new hooks, you can use the user_register and added_existing_user hooks to check $_POST, nonces, and update user meta, etc.

comment:25 @strangerstudios20 months ago

FYI, JJJ's patch might be outdated. I will get an updated one posted tomorrow.

@strangerstudios20 months ago

user_new_form_before and user_new_form_after hooks

comment:26 @strangerstudios20 months ago

18709.2 attached. This patch changes the names of the hooks added in the previous patch. The two hooks added now are:

  • user_new_form_before
  • user_new_form_after

First, I made both the network version and non-network version use the same set of hooks. The previous patch added new_user_form_before/after and add_user_form_before/after. I think this may have been a typo, but might have been intentional. If intentional, I think that it makes sense to use one set of hooks and let the plugin authors decide if they want to do something different for network sites and non-network sites. This is similar to the edit_user_profile hook which appears in the network and non-network version of user-edit.php.

I also switched the order from new_user to user_new to match the filename and another hook already in place there (user_new_form_tag).

I tested 18709.2 on the latest codebase (as of posting) and it works great. I'm open to double checks and ideas around the hook naming/etc.

comment:27 @strangerstudios20 months ago

  • Cc strangerstudios added
  • Version changed from 3.2.1 to 3.6

comment:28 follow-up: @ocean9020 months ago

  • Version changed from 3.6 to 3.2.1

Version field indicates when the enhancement was initially suggested.

comment:29 in reply to: ↑ 28 @strangerstudios20 months ago

Replying to ocean90:

Version field indicates when the enhancement was initially suggested.


comment:30 @MZAWeb20 months ago

Used the last patch for this http://bbpress.trac.wordpress.org/ticket/2223 and works great.

It'd be great to have this into 3.7

comment:31 @jeremyfelt20 months ago

  • Milestone changed from Future Release to 3.7

comment:32 @desrosj20 months ago

  • Cc desrosj@… added

comment:34 @DrewAPicture19 months ago

  • Keywords commit added; needs-docs removed

Since we're using the same two hooks in three different places, we should pass a context string with the actions to make targeting an 'Add Existing User' vs an 'Add New User' form a lot easier.

This is done + hook docs in 18709.3.patch.

@DrewAPicture19 months ago

hooks + passed context + hook docs

@DrewAPicture19 months ago


comment:35 @jeremyfelt18 months ago

Semantics...both actions fire inside the form, not before or after. Not sure if we have a pattern around that.

Seems ok to go in as it shouldn't have too much of an impact on any future revamps of the table structure in new/edit user. The latest patches don't add table rows. The only restricting aspect is that the actions both fire inside <form>, and before <submit>, but those shouldn't be a big deal.

comment:36 @jeremyfelt18 months ago

ignore the double submit

Looking at #BB2223, only the after hook is used. I wonder if that alone would be useful enough to enable plugins to introduce extra fields before the <submit>.

Last edited 18 months ago by jeremyfelt (previous) (diff)

comment:37 @helen18 months ago

I'm pretty wary of a hook firing before, given that all these forms are pretty normalized with username at top. One at the bottom, outside the table, seems okay, if maybe a little less than ideal. Can probably just be user_new_form in that case.

comment:38 @nacin18 months ago

  • Keywords needs-patch added; has-patch removed

Okay, let's add a single hook after the table, inside the form, before the submit button. Needs patch.

@jeremyfelt18 months ago

comment:39 @jeremyfelt18 months ago

  • Keywords has-patch added; needs-patch removed

18709.diff introduces one action, user_new_form.

comment:40 @helen18 months ago

  • Owner set to helen
  • Resolution set to fixed
  • Status changed from new to closed

In 25629:

Add a user_new_form hook, with context. props johnjamesjacoby, strangerstudios, jeremyfelt, DrewAPicture. fixes #18709.

comment:41 @nacin18 months ago

I'm reverting this from the network admin. It was modeled after the profile editing hooks, but the profile editor in the network admin is a direct inclusion of wp-admin/user-edit.php.

In this case, wp-admin/network/user-new.php is *not* the same as wp-admin/user-new.php. The function edit_user() is not used. We could rename the hook to network_user_new_form, but there is no obvious hook to manipulate the data that is coming in. wpmu_create_user() is used directly in this context.

wp-admin/user-new.php and wp-admin/network/user-new.php need a bit of an overhaul to clean up the supremely hacky multisite handling in both contexts. The network form is also a skeleton — user and email only. The context is significantly different, and while it can be handled with an is_network_admin() check, it's more likely that someone will attach a callback to user_new_form and end up creating a non-working field in the network admin.

comment:42 @nacin18 months ago

In 25631:

Remove the new user_new_form hook from the network admin, pending further review. see #18709.

comment:43 @jeremyfelt6 months ago

Related #13339, which requests a hook for the network admin new user form.

Note: See TracTickets for help on using tickets.