2014-02-19 22 views
0

我只是想将每个新用户的wordpress注册数据发送到Emailomat(API在这里:http://hikaru.cz/doc.htm)。但我不知道JSON,这个文档对我来说没有什么帮助。有人可以帮忙吗?从JSON API用户注册中获取数据

<?php 
/** 
* Used to be the page which displayed the registration form. 
* 
* This file is no longer used in WordPress and is 
* deprecated. 
* 
* @package WordPress 
* @deprecated Use wp_register() to create a registration link instead 
*/ 

require('./wp-load.php'); 
function register_new_user($user_login, $user_email, $nocomerce='') { 
    $errors = new WP_Error(); 

    $user_login = sanitize_user($user_login); 
    $user_email = apply_filters('user_registration_email', $user_email); 

    // Check the username 
    if ($user_login == '') 
     $errors->add('empty_username', __('<strong>ERROR</strong>: Please enter a username.')); 
    elseif (!validate_username($user_login)) { 
     $errors->add('invalid_username', __('<strong>ERROR</strong>: This username is invalid. Please enter a valid username.')); 
     $user_login = ''; 
    } elseif (username_exists($user_login)) 
     $errors->add('username_exists', __('<strong>ERROR</strong>: This username is already registered, please choose another one.')); 

    // Check the e-mail address 
    if ($user_email == '') { 
     $errors->add('empty_email', __('<strong>ERROR</strong>: Please type your e-mail address.')); 
    } elseif (!is_email($user_email)) { 
     $errors->add('invalid_email', __('<strong>ERROR</strong>: The email address isn&#8217;t correct.')); 
     $user_email = ''; 
    } elseif (email_exists($user_email)) 
     $errors->add('email_exists', __('<strong>ERROR</strong>: This email is already registered, please choose another one.')); 

    do_action('register_post', $user_login, $user_email, $errors); 

    $errors = apply_filters('registration_errors', $errors); 

    if ($errors->get_error_code()) 
    return $errors; 

    $user_pass = wp_generate_password(); 
    $user_id = wp_create_user($user_login, $user_pass, $user_email, $nocomerce); 
    if (!$user_id) { 
     $errors->add('registerfail', sprintf(__('<strong>ERROR</strong>: Couldn&#8217;t register you... please contact the <a href="mailto:%s">webmaster</a> !'), get_option('admin_email'))); 
     return $errors; 
    } 

    wp_new_user_notification($user_id, $user_pass); 

    return $user_id; 
} 
$registerfile = get_theme_root()."/".get_stylesheet()."/register.php"; 
if(file_exists($registerfile)) { 
    include $registerfile; 
} 
wp_redirect('wp-register.php?action=register'); 

?> 

我必须用这个做什么?

+0

在wordpress中使用JSON API插件吗? 如果你想得到json的细节,你需要使用钩子... –

+0

是的,我已经安装并激活,但我不明白它是如何工作的... – Medardaosa

+0

如果你不想阅读文档你至少应该考虑阅读你正在复制的文件的描述 - 尤其是它所说的“这个文件不再在WordPress中使用并且已被弃用的部分”。 – Chris

回答

1

JSON是JavaScript对象符号,这是非常容易从PHP中使用: 例如,如果你想传递一些参数,如用户注册,你可以这样做:

<script> 
    // From PHP 
    var data = <?php echo json_encode(array(
     'user_login' => $user_login, 
     'user_email' => $user_email 
    )); ?>; 

    alert("Hi " + data.user_login + " you email is: " + data.user_email); 

</script> 

的函数'json_encode'在PHP中将任何对象或数组转换为JSON作为字符串,你可以从你的JS脚本中读取。 我希望它能帮助你。

“对不起,我的英语”