I want to add some custom fields to add new user in Wordpress . I am using the following hooks:
show_user_profile
edit_user_profile
these hooks displaying new custom field on edit profile page, but i want the new custom field to be shown in Add New User page.
and also i want to insert values in wp_usermeta table, for this i am using following hooks:
personal_options_update
edit_user_profile_update
these hooks are also working fine on edit or update profile, but i need the insertion of record in wp_usermeta tabe at the time of Add new User, not at the Profile update time.
please give me hint of hook that will b used at ADD New User.
Thank you in advance.
Custom hooks are created and called in the same way that Core's hooks are, with add_action() / do_action() and add_filter() / apply_filters(). Since any plugin can create a custom hook, it's important to prefix your hook names to avoid collisions with other plugins.
In the Constructor When most of us learn how to use OOP PHP in a WordPress context, we generally learn to put hooks in the class constructor. Class constructors are magic methods that run when the class is instantiated. This pattern leads to the hooks being added magically.
Actions are one of the two types of Hooks. They provide a way for running a function at a specific point in the execution of WordPress Core, plugins, and themes. Callback functions for an Action do not return anything back to the calling Action hook. They are the counterpart to Filters.
Use this hook
add_action('user_new_form', 'xxxx');
Ok, here is the full code to add a permission checkbox for user mailChimp registration on user add/edit
//Add a mailchimp permission field, on user creation, user profile update
add_action('user_new_form', 'mailchimp_permission_field');
add_action('show_user_profile', 'mailchimp_permission_field');
add_action('edit_user_profile', 'mailchimp_permission_field');
function mailchimp_permission_field($user) {
?>
<table class="form-table">
<tr class="form-field">
<th scope="row"><label for="mail_chimp">Mail Chimp </label></th>
<td>
<label for="mail_chimp">
<input style="width: auto;" type="checkbox" name="mail_chimp" id="mail_chimp"
<?php if(current_filter() == 'user_new_form' || get_the_author_meta('mail_chimp', $user->ID )): ?>
checked = "checked"
<?php endif; ?> />
Subscribe to MailChimp.
</label>
</td>
</tr>
</table>
<?php }
// handle mailchimp registrations on user creation
add_action( 'user_register', 'subscribe_to_mailchimp_after_registration', 10, 1 );
function subscribe_to_mailchimp_after_registration( $user_id ) {
if (isset($_POST['email']) && isset($_POST['mail_chimp']) && $_POST['mail_chimp'] == 'on') {
mailchimp_subscribe($_POST['email']);
}
}
//Save new field for user in users_meta table
add_action('user_register', 'save_mailchimp_permission_field');
add_action('edit_user_profile_update', 'save_mailchimp_permission_field');
function save_mailchimp_permission_field($user_id) {
if (!current_user_can('edit_user', $user_id)) {
return false;
}
if (isset($_POST['mail_chimp']) && $_POST['mail_chimp'] == 'on') {
update_usermeta($user_id, 'mail_chimp', true);
mailchimp_subscribe(get_userdata($user_id)->user_email);
}
else {
update_usermeta($user_id, 'mail_chimp', false);
mailchimp_unsubscribe(get_userdata($user_id)->user_email);
}
}
As far as I can see there are no action hooks that will trigger on the new user page. Searched in user-new.php for do_action
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With