Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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 );
share|improve this question
    
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. –  t f Mar 14 '13 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. –  MF1 Mar 14 '13 at 23:12

1 Answer 1

up vote 1 down vote accepted

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 …
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.