0

I have a form in which user A with a custom role is able to invite other users. Javascript which handles that looks like this:

 sendNotificationEmail(newUserId) {
      newUserId = parseInt(newUserId);
      $.ajax({
       beforeSend: (xhr) => {
       xhr.setRequestHeader('X-WP-Nonce', ftsData.nonce);
       },
       url: ftsData.root_url + '/wp-admin/admin-ajax.php',
       type: 'POST',
       dataType: 'JSON',
       data: {
        action: 'send_it_bro',
        id : newUserId
       },
       success: (response) => {
        console.log(response);
       },
       error: (response) => {
        console.log(response);
       },
       });
    }

In functions.php I have:

add_action ('wp_ajax_send_it_bro', 'send_it_bro') ;

function send_it_bro(){
    $id = intval($_REQUEST['id']);
    wp_send_new_user_notifications($id, 'both');
    $return_value = $id ;
    wp_send_json_success ($return_value) ;
}

When user with a role "admin" invites new users everything works well, but when a user with custom role invites users, then they don't get welcome e-mail.

  • This looks like it uses the old Admin AJAX API, not the new REST API, I've updated the question to reflect that – Tom J Nowell Sep 3 '19 at 10:18
  • Yes, you're right. Can you tell me what should I do to make it work? – Bartomiej Rogacewicz bartekrog Sep 3 '19 at 10:30
  • I'm not sure why this happens, but, this is literally just the email notification, it won't actually invite the user. Also there's no security in your handler, I'd suggest moving to a REST endpoint and adding in some security options in the registration – Tom J Nowell Sep 3 '19 at 11:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.