简体   繁体   中英

wordpress : use insert_user() and wp_mail in the same function

today I have this issue : I'm making a little plugin to help the administrator of my website to take care of user accounts.

I'm pretty much done but I can't seem to be a able to create a new user (custom form using insert_user) and send a notification mail to this user (custom html mail with wp_mail) at the same time.

Here is my JS ajax function :

$(document).on('click','.nc_submit',function(){
    var ajaxurladmin = $('#urladminajax').val();

    var doc = $('.doc_holder .doc_input').val();
    var pass = $('.nc_pass').val();
    var name = $('.nc_name').val();
    var surname = $('.nc_surname').val();
    var email = $('.nc_email').val();
    var login = $('.nc_login').val();
    var client_type = $('.nc_client_type').val();
    var nicename = login.replace(' ','-');
    var display_name = name+' '+surname;
    var expiry_date = $('.nc_expiry').val();

    var today = new Date();
    var day = today.getDate();
    var month = today.getMonth()+1;
    var year = today.getFullYear();

    if(expiry_date == 'never'){
        var expiry = 'never';
    }else{
        if(expiry_date == 'year'){
            year++;
        }else if(expiry_date == 'month'){
            month++;
        }
        var expiry = day+'-'+month+'-'+year;
    }

    var str_taken_logins = $('.all_users').val();
    var taken_logins = str_taken_logins.split(',');

    the_message = $('.nc_message');
    the_loader = $('.nc_loading_cl');

    if($.inArray(login,taken_logins) >= 0){
        $('.login_exists').html('Ce login est déjà utilisé').animate({opacity:'1',width:'184px'},300).delay(2000).animate({opacity:'0',width:'0'},300);
    }else if(email == '' || login == '' || surname == ''){
        $('.login_exists').html('Un ou plusieurs champs requis ne sont pas remplis').animate({opacity:'1',width:'360px'},300).delay(2000).animate({opacity:'0',width:'0'},300);
    }else{
        the_loader.addClass('in_load');

        $.ajax({
        type: "POST",
        url: ajaxurladmin,
        data: {
            "action": "dbaddclient",
            "display_name":display_name,
            "nicename":nicename,
            "email":email,
            "login":login,
            "client_type":client_type,
            "pass":pass,
            "doc":doc,
            "expiry":expiry
        }
        }).done(function(msg){
            if(msg == 'update'){
                the_loader.removeClass('in_load');
                the_message.animate({opacity:'1',width:'135px'},300).delay(2000).animate({opacity:'0',width:'0'},300);

                location.reload();
            }
        });
    }
});

Nothing special to say here I think, it works fine.

Now my php :

function dbaddclient(){
    $display_name = $_POST["display_name"];
    $nicename = $_POST["nicename"];
    $email = $_POST["email"];
    $login = $_POST["login"];
    $client_type = $_POST["client_type"];
    $pass = $_POST["pass"];
    $doc = $_POST["doc"];
    $expiry = $_POST["expiry"];
    $userdata = array(
        'user_login'    =>  $login,
        'user_nicename'    =>  $nicename,
        'display_name'    =>  $display_name,
        'user_pass'    =>  $pass,
        'user_email'    =>  $email,
        'role'          =>  'Client'
     );
    global $wpdb;

    wp_insert_user($userdata);

    $wpdb->update('ar_users', array('client_type' => $client_type, 'clients_doc' => $doc, 'client_expiry' => $expiry), array('user_login' => $login));

    include('ar_mail.php');

    echo 'update';die;
}
add_action('wp_ajax_dbaddclient', 'dbaddclient');
add_action('wp_ajax_nopriv_dbaddclient', 'dbaddclient');

And lastly, my wp_mail function in my ar_mail.php file :

<?php

add_filter( 'wp_mail_content_type', 'wpdocs_set_html_mail_content_type' );

wp_mail($email,'Sender','<p>Bonjour '.$display_name.',</p><p>Votre compte ... a été activé.</p><p>Votre Login est : '.$login.'</p><p>Votre Mot de passe est : '.$pass.'</p>');

remove_filter( 'wp_mail_content_type', 'wpdocs_set_html_mail_content_type' );

function wpdocs_set_html_mail_content_type() {
    return 'text/html';
}
?>

What happens is that when I create a user, the email is sent properly ( and ends up in my spam box, if someone has a solution I'd be glad to hear it ), but the user isn't registered anymore (I checked my database, no sign of it).

I tried removing the "include('ar_mail.php');" part and then my user creation works fine...

Does anyone know how I could get through this ? I'd be reaaaally glad!

EDIT : I found the issue, here is what I should have done from the start :

<?php

add_filter( 'wp_mail_content_type', 'wpdocs_set_html_mail_content_type' );

$message = '<p>Bonjour '.$display_name.',</p><p>Votre compte ... a été activé.</p><p>Votre Login est : '.$login.'</p><p>Votre Mot de passe est : '.$pass.'</p>';

wp_mail($email,'Sender',$message);

remove_filter( 'wp_mail_content_type', 'wpdocs_set_html_mail_content_type' );

function wpdocs_set_html_mail_content_type() {
    return 'text/html';
}
?>

Simply make sure that the html code is passed to wp_mail as a variable.

Simple answer : You need to put the html body a variable before passing it to wp_mail().

Like this :

<?php

add_filter( 'wp_mail_content_type', 'wpdocs_set_html_mail_content_type' );

$message = '<p>Bonjour '.$display_name.',</p><p>Votre compte ... a été activé.</p><p>Votre Login est : '.$login.'</p><p>Votre Mot de passe est : '.$pass.'</p>';

wp_mail($email,'Sender',$message);

remove_filter( 'wp_mail_content_type', 'wpdocs_set_html_mail_content_type' );

function wpdocs_set_html_mail_content_type() {
return 'text/html';
}
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM