douze1332 2015-08-13 12:20
浏览 33

根据经过验证的电子邮件地址在WordPress中创建新用户

So I'm trying to make a plugin in WordPress that accepts a user's email, validates it, and if valid, create a new user (subscriber) in WordPress.

This part of the code creates the widget (works fine):

<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
<link rel="stylesheet" type="text/css" href="wp-content\plugins\more-subscribers\css\main.css">
<script type="text/javascript" src="wp-content\plugins\more-subscribers\js\assets.js"></script>

<?php
 /**
 * Initialize widgets
 **/     
    add_action('widgets_init', 'more_subscribers_init');     
 /**
 * Register widget
 **/    
    function more_subscribers_init()
    {
        register_widget(more_subscribers);
    }    
/**
 * Create the widget class 
 */
class more_subscribers extends WP_Widget
{  
    function more_subscribers()
    {
        $widget_options = array(
            'classname' => 'ms_class',
            'description' => 'Shows a field where the user can enter email address'
        );
        $this->WP_Widget('ms_id', 'More Subscribers', $widget_options);
    }
    /**
    * Show widget form in Appearance/Widgets 
    */
    function form($instance)
    {
        $ms_email = array('title' => 'Your email address');
        $instance = wp_parse_args( (array) $instance, $defaults);
        $title = esc_attr($instance['title']);
        echo '<p>Title <input type="text" class="widefat" name="'.$this->get_field_name('title').'" value="'.$title.'" /></p>';
    }
    /**
     * Save the widget 
     */
    function update ($new_instance, $old_instance)
    {
         $instance = $old_instance;
         $instance['title'] = strip_tags($new_instance['title']);
         return $instance;         
    }
    /**
     * Show widget in post/page 
    */
    function widget ($args, $instance)
    {
        extract($args);
        $title = apply_filters('widget_title', $instance['title']);            
        echo $before_widget;
        echo $before_title.$title.$after_title;            
        echo '<input type="email" name="FirstName" placeholder="Your email address" id="input_fld">';
        echo '<input type="Submit" name="submit_email" value="Submit" id="submit_btn">';                    
        //print widget
        echo $after_widget; 
     }
}
?>

This JS file validates the user's input. If the email is valid, it attempts to make an asynchronous .ajax connection to the php file which contains the function to create a new user using the provided email address (needs tuning):

//Random function to test if jQuery is working

//$('aside#ms_id-2 h2').hide();
//email validate function
function isValidEmailAddress(emailAddress)
{
    var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
    return pattern.test(emailAddress);
}
//Execute a php script on click event using an ajax request
 $(document).ready(function()
{
    $('#submit_btn').click(function()
    {
        var entered = $('#ms_id-2 > input#input_fld').val();
        if(isValidEmailAddress(entered) == false)
        {
            alert('Sorry, the email you entered is invalid. Please try again.');
        }
        else
        {
            var ajaxData = {
                action: 'add_subscriber',
                email: entered
            };
            //console.log(ajaxData);
            $.ajax({
                url: 'wp-content/plugins/more-subscribers/addEmail.php',
                type:'POST',
                data: ajaxData
                }).done(function(response){
                    alert('Thank you!');
                    //console.log(ajaxData);
                });                         
        }
    });

});

And lastly, the php file which should create the user in WP based on the data received from the .ajax request:

<?php
    add_action('wp_ajax_nopriv_add_subscriber','add_subscriber');
    //add_action('wp_ajax_add_subscriber','add_subscriber');
    /* Add email to database */
    function add_subscriber()
    {
        $email = htmlentities($_POST['email']);
        if(wp_create_user($email,'7777777',$email)) echo 'OK';
        die();
    }    
?> 

What am I doing wrong, the user is not being created. Please help me complete this code. I haven't been able to find any working examples anywhere.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 素材场景中光线烘焙后灯光失效
    • ¥15 请教一下各位,为什么我这个没有实现模拟点击
    • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
    • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
    • ¥20 有关区间dp的问题求解
    • ¥15 多电路系统共用电源的串扰问题
    • ¥15 slam rangenet++配置
    • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
    • ¥15 ubuntu子系统密码忘记
    • ¥15 保护模式-系统加载-段寄存器