所以我试图在WordPress中创建一个接受用户电子邮件的插件,验证它,如果有效,则在WordPress中创建一个新用户(订阅者)。
这部分代码创建了小部件(工作正常):
<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;
}
}
?>
此JS文件验证用户的输入。如果电子邮件有效,它会尝试与php文件建立异步.ajax连接,该文件包含使用提供的电子邮件地址创建新用户的功能(需要调整):
//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);
});
}
});
});
最后,php文件应根据从.ajax请求收到的数据在WP中创建用户:
<?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();
}
?>
我做错了什么,用户没有被创建。请帮我完成此代码。我无法在任何地方找到任何有用的例子。