bp_core_signup_user hook not working (PHP, BuddyPress, WordPress, & Parse.com)

I have been trying to hook into the WordPress registration action so that I can store the new user’s account info on my Parse.com User database. However, since I am using BuddyPress, the WP hook user_register does not seem to work.

After doing research online, it seems like I am supposed to use the BP hook bp_core_signup_user, but that does not seem to be working, and all the info online on how it should be implemented are years old and may be outdated. Problematically, BuddyPress does not have a good Codex at all, so I am kind of stuck. I’ve been at this for hours, but cannot figure it out.

Read More

This is the function I created in an attempt to hook into the registration process:

<?php
// Saves the newly registered BP user account to the Parse DB.
add_action('bp_core_signup_user', 'saveNewParseUser', 10, 5);

function saveNewParseUser($userId, $userLogin, $userPass, $userEmail, $userMeta) {
//Commit new user data to an HTTP POST request to Parse.
$url = 'https://api.parse.com/1/users';

$postdata = array(
    "wpUserId" => $userId,
    "username" => $userLogin,
    "password" => $userPass,
    "email" => $userEmail,
    "fullName" => $userMeta[display_name],
    "firstName" => $userMeta[first_name],
    "lastName" => $userMeta[last_name]
);
$appID = "a5TtlVG52JKTC************************";
$restAPIKey = "Qc86jA8dy1FpcB**************************";
$options = array();
$options[] = "Content-type: application/json";
$options[] = "X-Parse-Application-Id: $appID";
$options[] = "X-Parse-REST-API-Key: $restAPIKey";
$options[] = "X-Parse-Revocable-Session: 1";

//open connection
$ch = curl_init($url);

//sets the number of POST vars & POST data
curl_setopt_array($ch, array(
    CURLOPT_POST => true,
    CURLOPT_POSTFIELDS => json_encode($postdata),
    CURLOPT_HTTPHEADER => $options,
    CURLOPT_RETURNTRANSFER => true
));

//execute post
$result = curl_exec($ch);
$resultArray = json_decode($result, true);

//Error check
if (curl_errno($ch)) {
    echo "Error Code " . curl_errno() . ": " . curl_error($ch);
}

//Retrieve and place Parse user session token & ID into WP DB user account.
add_user_meta($userId, 'parseSessionToken', $resultArray[sessionToken]);
add_user_meta($userId, 'parseObjId', $resultArray[objectId]);
curl_close($ch);
}

What am I doing wrong? Is this not even being hooked and run as it is meant to?

I know it does not work because I check the Parse User DB after registering an account and a new row is not created, and the meta info I put into the WP account do not show there at all.

Interestingly, this DID work when I hooked into WP’s user_register (with the appropriate parameter and postdata array setup) when I included an exit; call at the end of the function, which essentially prevented the registration process from going through BuddyPress’ and its activation procedure, and instead went straight to registering through WordPress directly. That also left the web page displaying the output response from the HTTP request – it was the JSON response body as expected from Parse, so I know it did in fact work. Why would it be working when avoiding BuddyPress? BuddyPress seems to be causing the problem here. (If you would like to see the code for what I had done differently for this, then I can post it.)

Thank you for any assistance.

Related posts

1 comment

  1. I figured out what was wrong, and I feel like a complete idiot because it should have been obvious.

    There is nothing wrong with my function – I just did not realize the custom plugin it is contained in was disabled! Apparently that happened when I renamed the PHP file, deleted the one on the server, and put the new one in, before my question became an issue.

    I learned my lesson. And now I know that changing the plugin’s files will deactivate the plugin as a whole.

    As such, my hook to user_registration still works just fine, and it is not necessary to go through the bp_core_signup_user hook, so I have reverted to the former. For future reference for anyone wanting to know, this is my final function I used:

    <?php
    // Saves the newly registered WP user account to the Parse DB.
    add_action('user_register', 'saveNewParseUser');
    function saveNewParseUser($newUserId) {
    
    //Retrieve the new User object from WP's DB.
    $newUser = get_userdata($newUserId);
    
    //Commit new user data to an HTTP POST request to Parse.
    $url = 'https://api.parse.com/1/users';
    $postdata = array(
        "wpUserId" => $newUserId,
        "username" => $newUser->user_login,
        "password" => $newUser->user_pass,
        "email" => $newUser->user_email,
        "fullName" => $newUser->display_name,
        "firstName" => $newUser->first_name,
        "lastName" => $newUser->last_name
    );
    $appID = "a5TtlVG52JKTCbc*******************";
    $restAPIKey = "Qc86jA8dy1F************************";
    $options = array();
    $options[] = "Content-type: application/json";
    $options[] = "X-Parse-Application-Id: $appID";
    $options[] = "X-Parse-REST-API-Key: $restAPIKey";
    $options[] = "X-Parse-Revocable-Session: 1";
    
    //open connection
    $ch = curl_init($url);
    
    //sets the number of POST vars & POST data
    curl_setopt_array($ch, array(
        CURLOPT_POST => true,
        CURLOPT_POSTFIELDS => json_encode($postdata),
        CURLOPT_HTTPHEADER => $options,
        CURLOPT_RETURNTRANSFER => true
    ));
    
    //execute post
    $result = curl_exec($ch);
    $resultArray = json_decode($result, true);
    
    //Error check
    if (curl_errno($ch)) {
        echo "Error Code " . curl_errno() . ": " . curl_error($ch);
    }
    
    //Retrieve and place Parse user session token & ID into WP DB user account.
    add_user_meta($newUserId, 'parseSessionToken', $resultArray[sessionToken]);
    add_user_meta($newUserId, 'parseObjId', $resultArray[objectId]);
    
    curl_close($ch);
    }
    

Comments are closed.