|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Gravity Wiz // Gravity Forms // Disable Form Submission |
| 4 | + * https://gravitywiz.com |
| 5 | + * |
| 6 | + * Disables form submission entirely for specified forms. Useful for forms that act as calculators |
| 7 | + * (e.g. with GP Advanced Calculations) or forms that contain GC OpenAI Stream/Image fields. |
| 8 | + * |
| 9 | + * Instructions: |
| 10 | + * |
| 11 | + * 1. Install this code as a plugin or as a snippet (https://gravitywiz.com/documentation/how-do-i-install-a-snippet/) |
| 12 | + * 2. Specify the form IDs you would like to disable form submission at the bottom of this snippet. |
| 13 | + * |
| 14 | + * Plugin Name: Gravity Forms - Disable Form Submission |
| 15 | + * Plugin URI: http://gravitywiz.com/ |
| 16 | + * Description: Disables form submission entirely for specified forms. |
| 17 | + * Author: Gravity Wiz |
| 18 | + * Version: 0.1 |
| 19 | + * Author URI: http://gravitywiz.com |
| 20 | + */ |
| 21 | +class GW_Disable_Form_Submission { |
| 22 | + |
| 23 | + private $_args = array(); |
| 24 | + |
| 25 | + /** |
| 26 | + * @param $args array{ |
| 27 | + * form_ids: int[] |
| 28 | + * } The arguments to initialize GW_Disable_Form_Submission. |
| 29 | + */ |
| 30 | + public function __construct( $args = array() ) { |
| 31 | + $this->_args = wp_parse_args( $args, array( |
| 32 | + 'form_ids' => array(), |
| 33 | + ) ); |
| 34 | + |
| 35 | + add_action( 'init', array( $this, 'init' ) ); |
| 36 | + } |
| 37 | + |
| 38 | + public function init() { |
| 39 | + add_filter( 'gform_validation', array( $this, 'disable_form_submission' ) ); |
| 40 | + add_filter( 'gform_submit_button', array( $this, 'remove_submit_button' ), 10, 2 ); |
| 41 | + } |
| 42 | + |
| 43 | + public function disable_form_submission( $validation_result ) { |
| 44 | + if ( ! $this->is_applicable_form( $validation_result['form'] ) ) { |
| 45 | + return $validation_result; |
| 46 | + } |
| 47 | + |
| 48 | + $validation_result['is_valid'] = false; |
| 49 | + |
| 50 | + return $validation_result; |
| 51 | + } |
| 52 | + |
| 53 | + public function remove_submit_button( $button, $form ) { |
| 54 | + if ( ! $this->is_applicable_form( $form ) ) { |
| 55 | + return $button; |
| 56 | + } |
| 57 | + |
| 58 | + return ''; |
| 59 | + } |
| 60 | + |
| 61 | + public function is_applicable_form( $form ) { |
| 62 | + $form_id = isset( $form['id'] ) ? $form['id'] : $form; |
| 63 | + |
| 64 | + return in_array( $form_id, $this->_args['form_ids'], false ); |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +# Configuration |
| 69 | + |
| 70 | +new GW_Disable_Form_Submission(array( |
| 71 | + // replace with form IDs you would like to disable form submission for |
| 72 | + 'form_ids' => array( |
| 73 | + 6, |
| 74 | + ) |
| 75 | +)); |
0 commit comments