2 comments

  1. Your problem probably starts with your callback:

    public function handleAjax( $action )
    

    To send a JSON error or success, you just have to call

    wp_send_json_success( array( /* Data */ ) );
    wp_send_json_error( array( /* Data */ ) );
    

    The response will be like:

    {
        success : true,
        data : [
            // Some data as key/value pairs
        ]
    }
    

    Both functions internally call wp_send_json(). This sets the appropriate header:

    @header( 'Content-Type: application/json; charset=' . get_option( 'blog_charset' ) );
    

    and does the encoding to JSON for you:

    echo json_encode( $response );
    

    Your callback on the set Cookie filter won’t work as it will never be called. Better go one level up and use wp_set_auth_cookie(). Your user registration should already allow that as one of the later registration hooks already have the user ID present: user_register or wpmu_new_user should both work for that case.

    add_action( 'user_register', wpse136539userRegistration' );
    function wpse136539userRegistration( $id )
    {
        $user = get_user_by( 'id', $id );
    
        # @TODO Custom Error handling needed here
        if ( is_wp_error( $user ) )
            return;
    
        wp_set_current_user( $id );
        // Log the user in - set Cookie and let the browser remember it
        wp_set_auth_cookie( $id, TRUE );
    
        // Redirect user to his admin profile page ... @TODO maybe somewhere else
        exit( wp_safe_redirect( user_admin_url() ) );
    }
    
  2. If you’re trying to make the wordpress_logged_in_HASH cookie info available to javascript to set on the client side using javascript (and thus avoiding re-rendering the page) then I think you’ll have more success re-architecting to come up with another solution. The wordpress_logged_in cookie is a httponly cookie which means you don’t have access to it on the client, and shouldn’t expose it to javascript since this makes it vulnerable to XSS. See here for more details:
    http://blog.codinghorror.com/protecting-your-cookies-httponly/

Comments are closed.