field-advanced-link.php
6 years ago
field-button.php
6 years ago
field-clone.php
6 years ago
field-column.php
6 years ago
field-dynamic-message.php
6 years ago
field-file.php
6 years ago
field-flexible-content.php
6 years ago
field-forms.php
6 years ago
field-group.php
6 years ago
field-hidden.php
6 years ago
field-image.php
6 years ago
field-post-statuses.php
6 years ago
field-post-types.php
6 years ago
field-recaptcha.php
6 years ago
field-repeater.php
6 years ago
field-select.php
6 years ago
field-slug.php
6 years ago
field-taxonomies.php
6 years ago
field-taxonomy-terms.php
6 years ago
field-textarea.php
6 years ago
field-user-roles.php
6 years ago
field-button.php
201 lines
| 1 | <?php |
| 2 | |
| 3 | if(!defined('ABSPATH')) |
| 4 | exit; |
| 5 | |
| 6 | class acfe_field_button extends acf_field{ |
| 7 | |
| 8 | function __construct(){ |
| 9 | |
| 10 | $this->name = 'acfe_button'; |
| 11 | $this->label = __('Button', 'acfe'); |
| 12 | $this->category = 'basic'; |
| 13 | $this->defaults = array( |
| 14 | 'button_value' => __('Submit', 'acfe'), |
| 15 | 'button_type' => 'button', |
| 16 | 'button_before' => '', |
| 17 | 'button_after' => '', |
| 18 | 'button_class' => '', |
| 19 | 'button_id' => '', |
| 20 | ); |
| 21 | |
| 22 | parent::__construct(); |
| 23 | |
| 24 | } |
| 25 | |
| 26 | function render_field_settings($field){ |
| 27 | |
| 28 | // Value |
| 29 | acf_render_field_setting($field, array( |
| 30 | 'label' => __('Button value', 'acfe'), |
| 31 | 'instructions' => __('Set a default button value', 'acfe'), |
| 32 | 'type' => 'text', |
| 33 | 'name' => 'button_value', |
| 34 | 'default_value' => __('Submit', 'acfe') |
| 35 | )); |
| 36 | |
| 37 | // Type |
| 38 | acf_render_field_setting($field, array( |
| 39 | 'label' => __('Button value', 'acfe'), |
| 40 | 'instructions' => __('Choose the button type', 'acfe'), |
| 41 | 'type' => 'radio', |
| 42 | 'name' => 'button_type', |
| 43 | 'default_value' => 'button', |
| 44 | 'layout' => 'horizontal', |
| 45 | 'choices' => array( |
| 46 | 'button' => __('Button', 'acfe'), |
| 47 | 'submit' => __('Submit', 'acfe'), |
| 48 | ), |
| 49 | )); |
| 50 | |
| 51 | // class |
| 52 | acf_render_field_setting($field, array( |
| 53 | 'label' => __('Button attributes','acf'), |
| 54 | 'instructions' => '', |
| 55 | 'type' => 'text', |
| 56 | 'name' => 'button_class', |
| 57 | 'prepend' => __('class', 'acf'), |
| 58 | )); |
| 59 | |
| 60 | // id |
| 61 | acf_render_field_setting($field, array( |
| 62 | 'label' => '', |
| 63 | 'instructions' => '', |
| 64 | 'type' => 'text', |
| 65 | 'name' => 'button_id', |
| 66 | 'prepend' => __('id', 'acf'), |
| 67 | '_append' => 'button_class' |
| 68 | )); |
| 69 | |
| 70 | // Before HTML |
| 71 | acf_render_field_setting($field, array( |
| 72 | 'label' => __('Before HTML', 'acfe'), |
| 73 | 'instructions' => __('Custom HTML before the button', 'acfe'), |
| 74 | 'type' => 'textarea', |
| 75 | 'name' => 'button_before', |
| 76 | 'rows' => 4, |
| 77 | )); |
| 78 | |
| 79 | // After HTML |
| 80 | acf_render_field_setting($field, array( |
| 81 | 'label' => __('After HTML', 'acfe'), |
| 82 | 'instructions' => __('Custom HTML after the button', 'acfe'), |
| 83 | 'type' => 'textarea', |
| 84 | 'name' => 'button_after', |
| 85 | 'rows' => 4, |
| 86 | )); |
| 87 | |
| 88 | // Ajax |
| 89 | acf_render_field_setting($field, array( |
| 90 | 'label' => __('Ajax call', 'acfe'), |
| 91 | 'instructions' => __('Trigger ajax event on click', 'acfe'), |
| 92 | 'name' => 'button_ajax', |
| 93 | 'type' => 'true_false', |
| 94 | 'ui' => 1, |
| 95 | )); |
| 96 | |
| 97 | ob_start(); |
| 98 | ?> |
| 99 | Write your own Ajax code using the following hook:<br /><br /> |
| 100 | <pre> |
| 101 | add_action('wp_ajax_acfe/fields/button', 'my_acf_button_ajax'); |
| 102 | add_action('wp_ajax_nopriv_acfe/fields/button', 'my_acf_button_ajax'); |
| 103 | function my_acf_button_ajax(){ |
| 104 | |
| 105 | /** |
| 106 | * @bool/string $_POST['post_id'] Current post ID |
| 107 | * @string $_POST['field_key'] Button's field key |
| 108 | * @string $_POST['field_name'] Button's field name |
| 109 | */ |
| 110 | |
| 111 | echo 'Hello World'; |
| 112 | die; |
| 113 | |
| 114 | } |
| 115 | </pre> |
| 116 | <br /> |
| 117 | You can get access to Javascript ajax call using the following JS hooks:<br /><br /> |
| 118 | <pre> |
| 119 | acf.addAction('acfe/fields/button/before_ajax', function($el){ |
| 120 | // $el |
| 121 | }); |
| 122 | |
| 123 | acf.addAction('acfe/fields/button/ajax_success', function(response, $el){ |
| 124 | // response |
| 125 | // $el |
| 126 | }); |
| 127 | </pre> |
| 128 | <?php |
| 129 | |
| 130 | $message = ob_get_clean(); |
| 131 | |
| 132 | // ajax instructions |
| 133 | acf_render_field_setting($field, array( |
| 134 | 'label' => __('Ajax instructions','acf'), |
| 135 | 'instructions' => '', |
| 136 | 'type' => 'message', |
| 137 | 'name' => 'instructions', |
| 138 | 'message' => $message, |
| 139 | 'new_lines' => false, |
| 140 | 'conditional_logic' => array( |
| 141 | array( |
| 142 | array( |
| 143 | 'field' => 'button_ajax', |
| 144 | 'operator' => '==', |
| 145 | 'value' => '1', |
| 146 | ) |
| 147 | ) |
| 148 | ) |
| 149 | )); |
| 150 | |
| 151 | } |
| 152 | |
| 153 | function render_field($field){ |
| 154 | |
| 155 | // Before |
| 156 | if(isset($field['button_before']) && !empty($field['button_before'])){ |
| 157 | |
| 158 | echo $field['button_before']; |
| 159 | |
| 160 | } |
| 161 | |
| 162 | $ajax = false; |
| 163 | $button_ajax = $field['button_ajax']; |
| 164 | |
| 165 | if($button_ajax) |
| 166 | $ajax = 'data-ajax="1"'; |
| 167 | |
| 168 | // Button |
| 169 | if($field['button_type'] === 'button'){ |
| 170 | |
| 171 | echo '<button |
| 172 | id="' . esc_attr($field['button_id']) . '" |
| 173 | class="' . esc_attr($field['button_class']) . '" |
| 174 | ' . $ajax . ' |
| 175 | >' . esc_attr($field['button_value']) . '</button>'; |
| 176 | |
| 177 | // Submit |
| 178 | }elseif($field['button_type'] === 'submit'){ |
| 179 | |
| 180 | echo '<input |
| 181 | type="submit" |
| 182 | id="' . esc_attr($field['button_id']) . '" |
| 183 | class="' . esc_attr($field['button_class']) . '" |
| 184 | value="' . esc_attr($field['button_value']) . '" |
| 185 | ' . $ajax . ' |
| 186 | />'; |
| 187 | |
| 188 | } |
| 189 | |
| 190 | // After |
| 191 | if(isset($field['button_after']) && !empty($field['button_after'])){ |
| 192 | |
| 193 | echo $field['button_after']; |
| 194 | |
| 195 | } |
| 196 | |
| 197 | } |
| 198 | |
| 199 | } |
| 200 | |
| 201 | new acfe_field_button(); |