best practice : custom registration form

source : wp-hackers digest # 67,18,4.

[ A ] : I'm building my own template ( basically using the core wp-login template with a bunch of customizations ). This is how I'm doing a redirect, then I'm using some additional functions to call the correct template(s).

public function redirectAwayFromLoginPage() {
global $pagenow;
if( 'wp-login.php' == $pagenow ) {
$action = '?action=' . $_GET['action'];
wp_redirect( site_url( $this->acount_login_url . $action ) );
}

if ( $_GET['redirect_to'] == site_url('/wp-login.php') ) {
wp_redirect( site_url( $this->acount_login_url . '?action=register' ) );
exit();
}

}
add_action( 'init', array( $this,
'redirectAwayFromLoginPage' ) );

Something that I've seen people forget to do is filter the existing wp-core functions that build urls to login/reg/password/logout. i.e.

// Replace WP Login URIs
add_filter( 'logout_url', array( &$this, 'sv_string_replace_link' ) );
add_filter( 'login_url', array( $this, 'sv_string_replace_link' ) );
add_filter( 'lostpassword_url', array( $this, 'sv_string_replace_link' ) );
add_filter( 'register', array( $this, 'sv_string_replace_link' ) );

[ B ] : Issue : If I send a POST to wp-login.php using AJAX, I get an html page in response to my request, whereas I just need a valid/invalid flag, an array of errors ( if any ) and a redirect_to var ( if redirect is needed ).

Solution : If you're doing a login, then you send the log, pwd, and the redirect_to parameters. What you'll get back on success is a redirect to your redirect_to page, not HTML.

If you need to intercept that on the back end to check for something and/or return a different response, then you can use the login_redirect filter, which gets the redirect_to value as the first parameter, and the resulting $user object as the second parameter. To check for a login failure at that point, you'd check for is_wp_error($user) and if it's true, you can output your error stuff and then exit() cleanly. No HTML response of any sort then, as far as I can see.
 
 
Creative Commons License
This work by maniac.vardhan is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License.
 
 

0 comments :: best practice : custom registration form

Post a Comment