0

I created a plugin that adds a function to the registration process where the user info also gets added to an external db. The problem is that, when the function runs successfully i.e. user is able to be added to the external db, but the wordpress registration process returns and error, the user is still added to the external db. How can I structure this so that if there's an error on the Wordpress side or the external side, the user sees and error and nothing is added to any db.

Here's my code:

    function add_user_to_SF($errors, $sanitized_user_login, $user_email ) {
    global $SF_USERNAME;
    global $SF_PASSWORD;

    try {
      $mySforceConnection = new SforceEnterpriseClient();
      $mySoapClient = $mySforceConnection->createConnection(CD_PLUGIN_PATH . 'Toolkit/soapclient/enterprise.wsdl.xml');
      $mySFlogin = $mySforceConnection->login($SF_USERNAME, $SF_PASSWORD);

        $sObject = new stdclass();
        $sObject->FirstName = $_POST['user_login'];
        $sObject->LastName = $_POST['user_login'];
        $sObject->Email = $_POST['user_email'];

        $createResponse = $mySforceConnection->create(array($sObject), 'Contact');

        $ids = array();
            foreach ($createResponse as $createResult) {
                array_push($ids, $createResult->id);
            }

            } catch (Exception $e) {
              //echo $mySforceConnection->getLastRequest();
              //echo $e->faultstring;
              $errors->add( 'demo_error', __('<strong>ERROR</strong>: There is a Salesforce problem.','mydomain') );
              return $errors;
        }
              return $errors;
    }

    add_filter( 'registration_errors', 'add_user_to_SF', 10, 3 );
2
  • What is $createResult you use inside the if? I can't see that variable anywhere being declared - until the foreach, which is **after**/inside the if-else. Commented Mar 14, 2013 at 22:19
  • Right - I was trying to change the logic to accomplish what I'm trying to do.. I switched it back to how it was before. Commented Mar 14, 2013 at 23:12

1 Answer 1

1

The variable $errors is an instance of WP_Error. WordPress has done all internal logic before the filter is used, so you can check for existing errors:

function add_user_to_SF($errors, $sanitized_user_login, $user_email ) {

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

    global $SF_USERNAME;
    // continue with your custom code …

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.