2012-02-03 65 views
0

我有一个自定义注册表单。我试图在用户注册后将用户重定向到特定页面,但我尝试的方法没有奏效,这里是我的代码 - 在用户注册后如何获得重定向?:Wordpress - 自定义注册表格,重定向后

编辑: (一个jQuery或Javascript solutuion因为这将是太接受)

<?php 
/* Load registration file. */ 
require_once(ABSPATH . WPINC . '/registration.php'); 

/* If user registered, input info. */ 
if ('POST' == $_SERVER['REQUEST_METHOD'] && !empty($_POST['action']) && $_POST['action'] == 'adduser') { 
    $user_pass = wp_generate_password(); 
    $userdata = array(
     'user_pass' => esc_attr($_POST['user_pass']), 
     'user_login' => esc_attr($_POST['user_name']), 
     'user_email' => esc_attr($_POST['email']), 
    ); 

    if (!$userdata['user_login']) 
     $error = __('A username is required for registration.', 'frontendprofile'); 
    elseif (username_exists($userdata['user_login'])) 
     $error = __('Sorry, that username already exists!', 'frontendprofile'); 
    elseif (!is_email($userdata['user_email'], true)) 
     $error = __('You must enter a valid email address.', 'frontendprofile'); 
    elseif (email_exists($userdata['user_email'])) 
     $error = __('Sorry, that email address is already used!', 'frontendprofile'); 

    else{ 
     $new_user = wp_insert_user($userdata); 
     update_usermeta($new_user, 'fullname', esc_attr($_POST['fullname'] )); 
     update_usermeta($new_user, 'publication', esc_attr($_POST['publication'] )); 
     update_usermeta($new_user, 'usertype', esc_attr($_POST['usertype'] )); 
     wp_new_user_notification($new_user, $user_pass); 
     wp_redirect('http://www.google.com'); 
    } 
} 

?> 

<form method="post" id="adduser" class="user-forms" action="http://<?php echo $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; ?>"> 
    <p class="form-usertype"> 
    <strong>TYPE</strong> 
    <?php $usertype = get_the_author_meta('usertype', $current_user->ID); ?> 
    <ul> 
    <li> 
     <input class="radio" value="Media" name="usertype" <?php if ($_POST['usertype'] == 'Media') { ?>checked="checked"<?php }?> type="radio" /> 
     <span><?php _e('Media', 'frontendprofile'); ?></span> 
    </li> 
    <li> 
     <input class="radio" value="Freelance" name="usertype" <?php if ($_POST['usertype'] == 'Freelance' ) { ?>checked="checked"<?php }?> type="radio" /> 
     <span><?php _e('Freelance', 'frontendprofile'); ?></span> 
    </li> 
    <li> 
     <input class="radio" value="Student" name="usertype" <?php if ($_POST['usertype'] == 'Student') { ?>checked="checked"<?php }?> type="radio" /> 
     <span><?php _e('Student', 'frontendprofile'); ?></span> 
    </li> 
    </ul> 
    </p> 
    <!-- .form-usertype --> 

    <p class="form-username"> 
    <strong>FULL NAME</strong> 
    <input class="text-input" name="user_name" type="text" id="user_name" value="<?php if ($error) echo wp_specialchars($_POST['user_name'], 1); ?>" /> 
    </p> 
    <!-- .form-username --> 

    <p class="form-email"> 
    <strong>E-MAIL</strong> 
    <input class="text-input" name="email" type="text" id="email" value="<?php if ($error) echo wp_specialchars($_POST['email'], 1); ?>" /> 
    </p> 
    <!-- .form-email --> 

    <p class="form-password"> 
    <strong>CONFIRM E-MAIL</strong> 
    <input type="text" name="user_pass" id="user_pass" class="input" value="" size="20" tabindex="10" /> 
    </p> 

    <p class="form-publication"> 
    <strong>MEDIA OUTLET</strong> 
    <input class="text-input" name="publication" type="text" id="publication" value="<?php if ($error) echo wp_specialchars($_POST['publication'], 1); ?>"/> 
    </p> 
    <!-- .form-publication --> 

    <p class="form-submit"> <?php echo $referer; ?> 
    <input name="adduser" type="submit" id="addusersub" class="submit button" value="<?php if (current_user_can('create_users')) _e('Add User', 'frontendprofile'); else _e('Register', 'frontendprofile'); ?>" /> 
    <?php wp_nonce_field('add-user') ?> 
    <input name="action" type="hidden" id="action" value="adduser" /> 
    </p> 
    <!-- .form-submit --> 

</form> 

<script type="text/javascript"> 
$(document).ready(function(){ 

    $("input[name$='usertype']").click(function(){ 

    var radio_value = $(this).val(); 

    if(radio_value=='Media') { 
    $("p.form-publication").show(); 
    } 
    else if(radio_value=='Student') { 
    $("p.form-publication").hide(); 
    } 
    else if(radio_value=='Freelance') { 
    $("p.form-publication").hide(); 
    } 
    }); 
}); 
</script> 
+0

什么不管用?你有一个重定向到谷歌它不重定向? – Iznogood 2012-02-03 19:28:56

+0

是的重定向不起作用。 – 2012-02-03 19:46:51

回答

1

你撑着不尊重,你会正常使用预期的行动挂钩格式。你必须在这里做的是钩住你的插件的两个不同的功能。

一个人会在必要时绘制插件,例如使用简码。

另一个会挂钩wordpress的早期处理,以实际完成它要做的事情,比如添加一个用户。然后,如果您使用wp_redirect,它将允许您在将任何输出发送到浏览器之前重定向。

例如,在我的pluggins,我使用了钩:

add_action('init', 'my_function_to_process'); 
add_action('template_redirect', 'my_function_to_draw'); 

而且我创造这两个函数来完成他们在我撑着的文件做什么。

你可能想看看挂钩数据库API的更多信息,挂钩:

http://codex.wordpress.org/Plugin_API/Action_Reference

好运

编辑

只要改变:

wp_redirect('http://www.google.com'); 

为:

?><script>window.href.location = "http://www.google.com";</script><?php exit(); 

而且你会用自己的方式赢得胜利......这是在整合方面丑,我有一个撑着做了,但它应该工作的罚款...

+0

它不是一个插件。它是一个前端注册表单,允许在网站设计中进行注册,而不是重定向到wp-login.php注册。不知道这是否会改变任何东西:) – 2012-02-03 19:48:43

+0

仍然改变了一切,你必须找到一种方法来包括你的代码之前,wordpress绘图开始发生。如果处理代码发生在wordpress开始绘制之后,您不能重定向,因为标题已经发送了......当然,这不是一个插件,但您必须找到一种方法来加载该处理代码。一个插件不会是一个坏主意,因为插件早期加载... – 2012-02-03 19:51:52

+0

是否可以添加jQuery或按“提交后,发送用户到mywebpage.com”后按“提交”? – 2012-02-03 20:53:08