0

I wrote this function that adds data to an external db every time a custom post called 'event' is created. How can I prevent the save from happening if this function returns an error? At the moment, the info is not being added to the external db, and no error is being displayed even though I have a try/catch.

 function add_campaign_to_SF( $post_id) {

    if ('event' == $_POST['post-type']) {
        try {

              $sObject = new stdclass();
              $sObject->Name = get_the_title( $post_id );
              $sObject->StartDate = date("Y-m-d", strtotime($_POST["events_startdate"]));
              $sObject->EndDate = date("Y-m-d", strtotime($_POST["events_enddate"]));
              $sObject->IsActive = '1';

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

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


                } catch (Exception $e) {
                        echo $mySforceConnection->getLastRequest();
                        echo $e->faultstring;

                    }
    }
  }

 add_action( 'save_post', 'add_campaign_to_SF');
  • This is not a WP question. – vancoder Apr 8 '13 at 21:09
  • How is it not a Wordpress question when I'm asking about how to display an error during a wordpress process - creating a custom post? – MF1 Apr 8 '13 at 21:22
  • Because nothing in your question is specific to WordPress. Your problem is a PHP one. Please see the FAQ. – vancoder Apr 8 '13 at 21:30
  • This question is specific to the 'save_post' hook. Since when I run this outside of Wordpress is works flawlessly. – MF1 Apr 8 '13 at 21:33
1

WordPress post "saves" work by posting, processing, and redirecting back to the originating page. Put a die after your error and you should see your errors when appropriate. Without that, the page redirects before you see the error message.

In my opinion you would be better off turning on debugging and writing to the error log.

catch (Exception $e) {
    error_log($mySforceConnection->getLastRequest());
    error_log(echo $e->faultstring);
}

vancoder is correct that this is mostly a PHP problem, but given that I believe the issue to be related to WordPress' post/process/redirect pattern I thought an answer was in order. If I am right, that should straighten out the confusion.

Also, save_post runs after most of the post data has been saved so if you need to interrupt the primary save this is not the hook.

  • Thank you for your response. What would be a better hook to use if I did want to interrupt the save if my function returns an error? – MF1 Apr 8 '13 at 22:34
  • admin_init if you need to interrupt very early, or wp_insert_post_data if you want to interrupt after some of the processing is done but not saved. It depends on context. – s_ha_dum Apr 22 '13 at 17:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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