Contact Form 7 via admin-ajax.php
-
Hi. I needed to make some AJAX requests via admin-ajax.php, which would contain a Contact Form 7 form in the response. Currently, CF7 will not work with admin-ajax.php as that file defines WP_ADMIN to true, and this constant is the sole decider to determine whether CF7 should load its admin.php or controller.php. I have patched CF7 to also check the DOING_AJAX constant that is defined in admin-ajax.php, so it will work with AJAX requests done via admin-ajax.php. I also had to patch CF7’s script.js to tell the jQuery Form Plugin to use delegation – which allows the event handlers to work in a form that is added to the page with AJAX. I have pasted a copy of the diff making these two changes below. Please let me know if there is any reason why this is not a good idea, otherwise it would be great to see these changes in a future version ??
diff -rupN contact-form-7.3.5.4/includes/js/scripts.js contact-form-7/includes/js/scripts.js --- contact-form-7.3.5.4/includes/js/scripts.js 2013-11-11 23:59:14.000000000 -0500 +++ contact-form-7/includes/js/scripts.js 2013-12-06 11:17:53.000000000 -0500 @@ -23,6 +23,7 @@ }, data: { '_wpcf7_is_ajax_call': 1 }, dataType: 'json', + delegation: true, success: function(data) { if (! $.isPlainObject(data) || $.isEmptyObject(data)) return; diff -rupN contact-form-7.3.5.4/settings.php contact-form-7/settings.php --- contact-form-7.3.5.4/settings.php 2013-11-11 23:59:14.000000000 -0500 +++ contact-form-7/settings.php 2013-12-06 11:17:53.000000000 -0500 @@ -8,7 +8,7 @@ require_once WPCF7_PLUGIN_DIR . '/includ require_once WPCF7_PLUGIN_DIR . '/includes/capabilities.php'; require_once WPCF7_PLUGIN_DIR . '/includes/classes.php'; -if ( is_admin() ) +if ( is_admin() && (!defined('DOING_AJAX') || !DOING_AJAX) ) require_once WPCF7_PLUGIN_DIR . '/admin/admin.php'; else require_once WPCF7_PLUGIN_DIR . '/includes/controller.php';
- The topic ‘Contact Form 7 via admin-ajax.php’ is closed to new replies.