Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ### In my page template ###
- <?php
- acf_form_head();
- print '<div id="acf-messages"></div>';
- acf_form(
- array(
- 'post_id' => 'new_post',
- 'form_attributes' => array(
- 'id' => 'checklist-new',
- ),
- 'post_title' => true,
- 'post_content' => false,
- 'new_post' => array(
- 'post_type' => $type,
- 'post_status' => 'publish'
- ),
- 'submit_value' => 'Add the Checklist',
- )
- );
- checklists_add_initialisation_js('checklist-new');
- ?>
- ### In functions.php ###
- <?php
- function checklists_add_initialisation_js($form_id) {
- $camelcase = 'Checklist' . str_replace(" ", '', ucwords(str_replace("-", ' ', $form_id)));
- ?>
- <script type="text/javascript">
- (function($) {
- jQuery('#<?php print $form_id; ?> .acf-form-submit input[type=submit]').attr('data-saving-text', 'Saving ...');
- 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'));
- jQuery('#<?php print $form_id; ?> .acf-form-submit input[type=submit]').on('click', function(e) {
- e.preventDefault();
- console.log(typeof jQuery.<?php print $camelcase; ?>);
- var proceed = true;
- if (typeof jQuery.<?php print $camelcase; ?> != 'undefined') {
- proceed = jQuery.<?php print $camelcase; ?>('presubmit');
- }
- if (proceed) {
- var acfForm = jQuery('#<?php print $form_id; ?>');
- var submitBtn = jQuery(this);
- var messageBox = jQuery('#acf-messages');
- submitBtn.prop('disabled', true);
- submitBtn.button('saving');
- acfForm.block({
- message: '<p>Processing ...</p>',
- overlayCSS: {
- backgroundColor: '#fff'
- }
- });
- var post_data = jQuery('#<?php print $form_id; ?>').serialize() + '&checklists_return=json';
- post_data['action'] = 'acf/validate_save_post';
- post_data['lsb_return'] = 'json';
- jQuery.post('/wp-admin/admin-ajax.php?action=checklist_submit&json=true', post_data, null, 'json').done(function(data) {
- acfForm.unblock();
- if (data.success == false || typeof data.success == 'undefined') {
- messageBox.addClass('alert alert-warning');
- var html = '<p><strong>There were some problems when saving:</strong></p><ul>';
- for (i in data.errors) {
- html = html + '<li>' + data.errors[i].message + '</li>';
- }
- html = html + '</ul>';
- messageBox.html(html);
- $('html, body').animate({
- scrollTop: jQuery('.entry-header').offset().top
- }, 1000);
- submitBtn.button('reset');
- submitBtn.prop('disabled', false);
- }
- else {
- messageBox.removeClass('alert-warning');
- messageBox.addClass('alert alert-success');
- var html = '<p>Saved</p>';
- messageBox.html(html);
- submitBtn.button('saved');
- $('html, body').animate({
- scrollTop: jQuery('.entry-header').offset().top
- }, 1000);
- submitBtn.prop('disabled', false);
- }
- });
- }
- });
- })(jQuery);
- </script>
- <?php
- }
- add_action('wp_ajax_nopriv_checklist_submit', 'acf_form_head');
- add_action('wp_ajax_checklist_submit','acf_form_head');
- add_action( 'wp_ajax_nopriv_checklists_load_edit_form', 'checklists_load_edit_form' );
- add_action( 'wp_ajax_checklists_load_edit_form','checklists_load_edit_form' );
- if (isset($_POST['checklists_return']) && $_POST['checklists_return'] == 'json') {
- add_action('acf/validate_save_post', 'checklists_validation_json', 99);
- add_action('acf/save_post', 'checklists_save_json', 99);
- }
- function checklists_validation_json() {
- $errors = acf_get_validation_errors();
- if ($errors) {
- $response = new \stdClass;
- $response->success = false;
- $response->errors = $errors;
- wp_send_json($response);
- exit;
- }
- return true;
- }
- function checklists_save_json() {
- $response = new \stdClass;
- $response->success = true;
- wp_send_json($response);
- exit;
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement