preface
I have written about the plug-in of WeChat official account verification code function before, and recently found that it can also be improved. Of course, I haven't actually changed this plug-in, just to say the following
idea
Implementation code
//Generate WeChat verification code function wx_captcha() { date_default_timezone_set('Asia/Shanghai'); $min = floor(date("i") / 5); $day = date("d"); $day = ltrim($day, 0); $url = get_permalink(); $wx_token = trim('xxxxoooootoken');/* WeChat token*/ $ua = $_SERVER['HTTP_USER_AGENT']; $ip = $_SERVER['HTTP_CLIENT_IP']; $captcha = sha1($min . $url . $wx_token.$ua.$ip); $captcha = substr($captcha, $day, 6); return $captcha; } //Short code for generating verification code function wx_captcha_gen_btn() { return '<a id="wx_captcha_gen_btn" href="javascript:; "Data action=" wx_captcha_gen "class=" cm btn primary wx_captcha_gen_btn ">Click to load the verification code</a>';; } add_shortcode('wx_captcha_btn', 'wx_captcha_gen_btn'); //Accept ajajx information function wx_captcha_gen() { if (isset($_POST['action']) && $_POST['action'] == 'wx_captcha_gen') { exit(wx_captcha()); } else { exit('400'); } } add_action('wp_ajax_wx_captcha_gen', 'wx_captcha_gen'); add_action('wp_ajax_nopriv_wx_captcha_gen', 'wx_captcha_gen');
$("#wx_captcha_gen_btn").click(function() { var ajax_data = { action: $("#wx_captcha_gen_btn").data("action") }; $.post(ajaxurl, ajax_data,function(b){ b = $.trim(b); // Login information if (b !== ' 400' && b == '200') { $('#wx_captcha_gen_btn').after(b);// Insert verification code character $. post (ajaxurl, 'checkmp', function (c) {//Start checking whether the public account receives this verification code c = $.trim(c); // Login information if (c !== ' 400 '&&c=='200') {//If information is received $load_content();// Load hidden content } else { Alert ("An error occurred"); } }); } else { Alert ("An error occurred"); } }); });
Comb