Advertisement
logicalor

ACF Frontend Form AJAX Override

May 5th, 2015
579
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.21 KB | None | 0 0
  1. ### In my page template ###
  2.  
  3. <?php
  4.  
  5. acf_form_head();
  6.  
  7. print '<div id="acf-messages"></div>';
  8.  
  9. acf_form(
  10.   array(
  11.     'post_id' => 'new_post',
  12.     'form_attributes' => array(
  13.       'id' => 'checklist-new',
  14.     ),
  15.     'post_title' => true,
  16.     'post_content' => false,
  17.     'new_post' => array(
  18.       'post_type' => $type,
  19.       'post_status' => 'publish'
  20.     ),
  21.     'submit_value' => 'Add the Checklist',
  22.   )
  23. );
  24.  
  25. checklists_add_initialisation_js('checklist-new');
  26.  
  27. ?>
  28.  
  29. ### In functions.php ###
  30.  
  31. <?php
  32.  
  33. function checklists_add_initialisation_js($form_id) {
  34.  
  35.   $camelcase = 'Checklist' . str_replace(" ", '', ucwords(str_replace("-", ' ', $form_id)));
  36.  
  37. ?>
  38. <script type="text/javascript">
  39. (function($) {
  40.   jQuery('#<?php print $form_id; ?> .acf-form-submit input[type=submit]').attr('data-saving-text', 'Saving ...');
  41.   jQuery('#<?php print $form_id; ?> .acf-form-submit input[type=submit]').attr('data-saved-text', jQuery('#<?php print $form_id; ?> .acf-form-submit input[type=submit]').prop('value'));
  42.  
  43.   jQuery('#<?php print $form_id; ?> .acf-form-submit input[type=submit]').on('click', function(e) {
  44.     e.preventDefault();
  45.     console.log(typeof jQuery.<?php print $camelcase; ?>);
  46.     var proceed = true;
  47.     if (typeof jQuery.<?php print $camelcase; ?> != 'undefined') {
  48.       proceed = jQuery.<?php print $camelcase; ?>('presubmit');
  49.     }
  50.     if (proceed) {
  51.       var acfForm = jQuery('#<?php print $form_id; ?>');
  52.       var submitBtn = jQuery(this);
  53.       var messageBox = jQuery('#acf-messages');
  54.       submitBtn.prop('disabled', true);
  55.       submitBtn.button('saving');
  56.       acfForm.block({
  57.         message: '<p>Processing ...</p>',
  58.         overlayCSS: {
  59.           backgroundColor: '#fff'
  60.         }
  61.       });
  62.       var post_data = jQuery('#<?php print $form_id; ?>').serialize() + '&checklists_return=json';
  63.       post_data['action'] = 'acf/validate_save_post';
  64.       post_data['lsb_return'] = 'json';
  65.      
  66.       jQuery.post('/wp-admin/admin-ajax.php?action=checklist_submit&json=true', post_data, null, 'json').done(function(data) {
  67.         acfForm.unblock();
  68.         if (data.success == false || typeof data.success == 'undefined') {
  69.          
  70.           messageBox.addClass('alert alert-warning');
  71.            
  72.           var html = '<p><strong>There were some problems when saving:</strong></p><ul>';
  73.            
  74.           for (i in data.errors) {
  75.             html = html + '<li>' + data.errors[i].message + '</li>';
  76.           }
  77.            
  78.           html = html + '</ul>';
  79.            
  80.           messageBox.html(html);
  81.  
  82.           $('html, body').animate({
  83.             scrollTop: jQuery('.entry-header').offset().top
  84.           }, 1000);
  85.           submitBtn.button('reset');
  86.           submitBtn.prop('disabled', false);
  87.         }
  88.         else {
  89.  
  90.           messageBox.removeClass('alert-warning');
  91.           messageBox.addClass('alert alert-success');
  92.            
  93.           var html = '<p>Saved</p>';
  94.            
  95.           messageBox.html(html);
  96.          
  97.           submitBtn.button('saved');
  98.           $('html, body').animate({
  99.             scrollTop: jQuery('.entry-header').offset().top
  100.           }, 1000);
  101.           submitBtn.prop('disabled', false);  
  102.         }
  103.       });
  104.     }
  105.   });
  106.        
  107. })(jQuery);    
  108. </script>
  109. <?php
  110. }
  111.  
  112. add_action('wp_ajax_nopriv_checklist_submit',  'acf_form_head');
  113. add_action('wp_ajax_checklist_submit','acf_form_head');
  114.  
  115. add_action( 'wp_ajax_nopriv_checklists_load_edit_form',  'checklists_load_edit_form' );
  116. add_action( 'wp_ajax_checklists_load_edit_form','checklists_load_edit_form' );
  117.  
  118. if (isset($_POST['checklists_return']) && $_POST['checklists_return'] == 'json') {
  119.   add_action('acf/validate_save_post', 'checklists_validation_json', 99);
  120.   add_action('acf/save_post', 'checklists_save_json', 99);
  121. }
  122.  
  123. function checklists_validation_json() {
  124.   $errors = acf_get_validation_errors();
  125.   if ($errors) {
  126.    
  127.     $response = new \stdClass;
  128.     $response->success = false;
  129.     $response->errors = $errors;
  130.    
  131.     wp_send_json($response);
  132.     exit;
  133.   }
  134.   return true;
  135. }
  136.  
  137. function checklists_save_json() {
  138.  
  139.   $response = new \stdClass;
  140.   $response->success = true;
  141.  
  142.   wp_send_json($response);
  143.   exit;
  144. }
  145.  
  146. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement