├── .gitignore ├── README.md ├── acf-auto_generated_value-v4.php ├── acf-auto_generated_value-v5.php ├── acf-auto_generated_value.php ├── css ├── README.md └── input.css ├── images └── README.md ├── js ├── README.md └── input.js ├── lang └── README.md └── readme.txt /.gitignore: -------------------------------------------------------------------------------- 1 | # OS generated files # 2 | ###################### 3 | .DS_Store 4 | .DS_Store? 5 | ._* 6 | .Spotlight-V100 7 | .Trashes 8 | ehthumbs.db 9 | Thumbs.db 10 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ACF Auto-generated value Field 2 | 3 | ----------------------- 4 | 5 | ### Creates a hidden/readonly field in a repeater that gets a unique key, that won't change once it is saved. Ideal for user-generated forms. Includes option to hide the field from the user 6 | 7 | 8 | ### Compatibility 9 | 10 | This ACF field type is compatible with: 11 | * ACF 5 12 | 13 | ### Installation 14 | 15 | 1. Copy the `acf-auto_generated_value` folder into your `wp-content/plugins` folder 16 | 2. Activate the Auto-generated value plugin via the plugins admin page 17 | 3. Create a new field via ACF and select the Auto-generated value type 18 | 4. Please refer to the Creates a hidden/readonly field in a repeater that gets a unique key, that won't change once it is saved. Ideal for user-generated forms. for more info regarding the field type settings 19 | 20 | ### Changelog 21 | Please see `readme.txt` for changelog 22 | -------------------------------------------------------------------------------- /acf-auto_generated_value-v4.php: -------------------------------------------------------------------------------- 1 | name = 'auto_generated_value'; 23 | $this->label = __('Auto-generated value'); 24 | $this->category = __("Basic",'acf'); // Basic, Content, Choice, etc 25 | $this->defaults = array( 26 | // add default here to merge into your field. 27 | // This makes life easy when creating the field options as you don't need to use any if( isset('') ) logic. eg: 28 | //'preview_size' => 'thumbnail' 29 | ); 30 | 31 | 32 | // do not delete! 33 | parent::__construct(); 34 | 35 | 36 | // settings 37 | $this->settings = array( 38 | 'path' => apply_filters('acf/helpers/get_path', __FILE__), 39 | 'dir' => apply_filters('acf/helpers/get_dir', __FILE__), 40 | 'version' => '1.0.0' 41 | ); 42 | 43 | } 44 | 45 | 46 | /* 47 | * create_options() 48 | * 49 | * Create extra options for your field. This is rendered when editing a field. 50 | * The value of $field['name'] can be used (like below) to save extra data to the $field 51 | * 52 | * @type action 53 | * @since 3.6 54 | * @date 23/01/13 55 | * 56 | * @param $field - an array holding all the field's data 57 | */ 58 | 59 | function create_options( $field ) 60 | { 61 | // defaults? 62 | /* 63 | $field = array_merge($this->defaults, $field); 64 | */ 65 | 66 | // key is needed in the field names to correctly save the data 67 | $key = $field['name']; 68 | 69 | 70 | // Create Field Options HTML 71 | ?> 72 | 73 | 74 | 75 |

76 | 77 | 78 | 'radio', 82 | 'name' => 'fields['.$key.'][preview_size]', 83 | 'value' => $field['preview_size'], 84 | 'layout' => 'horizontal', 85 | 'choices' => array( 86 | 'thumbnail' => __('Thumbnail'), 87 | 'something_else' => __('Something Else'), 88 | ) 89 | )); 90 | 91 | ?> 92 | 93 | 94 | defaults, $field); 116 | */ 117 | 118 | // perhaps use $field['preview_size'] to alter the markup? 119 | 120 | 121 | // create Field HTML 122 | ?> 123 |
124 | 125 |
126 | settings['dir'] . 'js/input.js', array('acf-input'), $this->settings['version'] ); 149 | wp_register_style( 'acf-input-auto_generated_value', $this->settings['dir'] . 'css/input.css', array('acf-input'), $this->settings['version'] ); 150 | 151 | 152 | // scripts 153 | wp_enqueue_script(array( 154 | 'acf-input-auto_generated_value', 155 | )); 156 | 157 | // styles 158 | wp_enqueue_style(array( 159 | 'acf-input-auto_generated_value', 160 | )); 161 | 162 | 163 | } 164 | 165 | 166 | /* 167 | * input_admin_head() 168 | * 169 | * This action is called in the admin_head action on the edit screen where your field is created. 170 | * Use this action to add CSS and JavaScript to assist your create_field() action. 171 | * 172 | * @info http://codex.wordpress.org/Plugin_API/Action_Reference/admin_head 173 | * @type action 174 | * @since 3.6 175 | * @date 23/01/13 176 | */ 177 | 178 | function input_admin_head() 179 | { 180 | // Note: This function can be removed if not used 181 | } 182 | 183 | 184 | /* 185 | * field_group_admin_enqueue_scripts() 186 | * 187 | * This action is called in the admin_enqueue_scripts action on the edit screen where your field is edited. 188 | * Use this action to add CSS + JavaScript to assist your create_field_options() action. 189 | * 190 | * $info http://codex.wordpress.org/Plugin_API/Action_Reference/admin_enqueue_scripts 191 | * @type action 192 | * @since 3.6 193 | * @date 23/01/13 194 | */ 195 | 196 | function field_group_admin_enqueue_scripts() 197 | { 198 | // Note: This function can be removed if not used 199 | } 200 | 201 | 202 | /* 203 | * field_group_admin_head() 204 | * 205 | * This action is called in the admin_head action on the edit screen where your field is edited. 206 | * Use this action to add CSS and JavaScript to assist your create_field_options() action. 207 | * 208 | * @info http://codex.wordpress.org/Plugin_API/Action_Reference/admin_head 209 | * @type action 210 | * @since 3.6 211 | * @date 23/01/13 212 | */ 213 | 214 | function field_group_admin_head() 215 | { 216 | // Note: This function can be removed if not used 217 | } 218 | 219 | 220 | /* 221 | * load_value() 222 | * 223 | * This filter is applied to the $value after it is loaded from the db 224 | * 225 | * @type filter 226 | * @since 3.6 227 | * @date 23/01/13 228 | * 229 | * @param $value - the value found in the database 230 | * @param $post_id - the $post_id from which the value was loaded 231 | * @param $field - the field array holding all the field options 232 | * 233 | * @return $value - the value to be saved in the database 234 | */ 235 | 236 | function load_value( $value, $post_id, $field ) 237 | { 238 | // Note: This function can be removed if not used 239 | return $value; 240 | } 241 | 242 | 243 | /* 244 | * update_value() 245 | * 246 | * This filter is applied to the $value before it is updated in the db 247 | * 248 | * @type filter 249 | * @since 3.6 250 | * @date 23/01/13 251 | * 252 | * @param $value - the value which will be saved in the database 253 | * @param $post_id - the $post_id of which the value will be saved 254 | * @param $field - the field array holding all the field options 255 | * 256 | * @return $value - the modified value 257 | */ 258 | 259 | function update_value( $value, $post_id, $field ) 260 | { 261 | // Note: This function can be removed if not used 262 | return $value; 263 | } 264 | 265 | 266 | /* 267 | * format_value() 268 | * 269 | * This filter is applied to the $value after it is loaded from the db and before it is passed to the create_field action 270 | * 271 | * @type filter 272 | * @since 3.6 273 | * @date 23/01/13 274 | * 275 | * @param $value - the value which was loaded from the database 276 | * @param $post_id - the $post_id from which the value was loaded 277 | * @param $field - the field array holding all the field options 278 | * 279 | * @return $value - the modified value 280 | */ 281 | 282 | function format_value( $value, $post_id, $field ) 283 | { 284 | // defaults? 285 | /* 286 | $field = array_merge($this->defaults, $field); 287 | */ 288 | 289 | // perhaps use $field['preview_size'] to alter the $value? 290 | 291 | 292 | // Note: This function can be removed if not used 293 | return $value; 294 | } 295 | 296 | 297 | /* 298 | * format_value_for_api() 299 | * 300 | * This filter is applied to the $value after it is loaded from the db and before it is passed back to the API functions such as the_field 301 | * 302 | * @type filter 303 | * @since 3.6 304 | * @date 23/01/13 305 | * 306 | * @param $value - the value which was loaded from the database 307 | * @param $post_id - the $post_id from which the value was loaded 308 | * @param $field - the field array holding all the field options 309 | * 310 | * @return $value - the modified value 311 | */ 312 | 313 | function format_value_for_api( $value, $post_id, $field ) 314 | { 315 | // defaults? 316 | /* 317 | $field = array_merge($this->defaults, $field); 318 | */ 319 | 320 | // perhaps use $field['preview_size'] to alter the $value? 321 | 322 | 323 | // Note: This function can be removed if not used 324 | return $value; 325 | } 326 | 327 | 328 | /* 329 | * load_field() 330 | * 331 | * This filter is applied to the $field after it is loaded from the database 332 | * 333 | * @type filter 334 | * @since 3.6 335 | * @date 23/01/13 336 | * 337 | * @param $field - the field array holding all the field options 338 | * 339 | * @return $field - the field array holding all the field options 340 | */ 341 | 342 | function load_field( $field ) 343 | { 344 | // Note: This function can be removed if not used 345 | return $field; 346 | } 347 | 348 | 349 | /* 350 | * update_field() 351 | * 352 | * This filter is applied to the $field before it is saved to the database 353 | * 354 | * @type filter 355 | * @since 3.6 356 | * @date 23/01/13 357 | * 358 | * @param $field - the field array holding all the field options 359 | * @param $post_id - the field group ID (post_type = acf) 360 | * 361 | * @return $field - the modified field 362 | */ 363 | 364 | function update_field( $field, $post_id ) 365 | { 366 | // Note: This function can be removed if not used 367 | return $field; 368 | } 369 | 370 | 371 | } 372 | 373 | 374 | // create field 375 | new acf_field_auto_generated_value(); 376 | 377 | ?> 378 | -------------------------------------------------------------------------------- /acf-auto_generated_value-v5.php: -------------------------------------------------------------------------------- 1 | name = 'auto_generated_value'; 26 | 27 | 28 | /* 29 | * label (string) Multiple words, can include spaces, visible when selecting a field type 30 | */ 31 | 32 | $this->label = __('Auto-generated value', 'acf-auto_generated_value'); 33 | 34 | 35 | /* 36 | * category (string) basic | content | choice | relational | jquery | layout | CUSTOM GROUP NAME 37 | */ 38 | 39 | $this->category = 'basic'; 40 | 41 | 42 | /* 43 | * defaults (array) Array of default settings which are merged into the field object. These are used later in settings 44 | */ 45 | 46 | $this->defaults = array( 47 | 'value_prefix' => 'my_key_', 48 | ); 49 | 50 | 51 | /* 52 | * l10n (array) Array of strings that are used in JavaScript. This allows JS strings to be translated in PHP and loaded via: 53 | * var message = acf._e('auto_generated_value', 'error'); 54 | */ 55 | 56 | $this->l10n = array( 57 | 'error' => __('Error! Please enter a higher value', 'acf-auto_generated_value'), 58 | ); 59 | 60 | 61 | // do not delete! 62 | parent::__construct(); 63 | 64 | } 65 | 66 | 67 | /* 68 | * render_field_settings() 69 | * 70 | * Create extra settings for your field. These are visible when editing a field 71 | * 72 | * @type action 73 | * @since 3.6 74 | * @date 23/01/13 75 | * 76 | * @param $field (array) the $field being edited 77 | * @return n/a 78 | */ 79 | 80 | function render_field_settings( $field ) { 81 | 82 | /* 83 | * acf_render_field_setting 84 | * 85 | * This function will create a setting for your field. Simply pass the $field parameter and an array of field settings. 86 | * The array of settings does not require a `value` or `prefix`; These settings are found from the $field array. 87 | * 88 | * More than one setting can be added by copy/paste the above code. 89 | * Please note that you must also have a matching $defaults value for the field name (font_size) 90 | */ 91 | 92 | acf_render_field_setting( $field, array( 93 | 'label' => __('Prefix','acf-auto_generated_value'), 94 | 'instructions' => __('Set the prefix of the generated key','acf-auto_generated_value'), 95 | 'type' => 'text', 96 | 'name' => 'value_prefix', 97 | )); 98 | 99 | acf_render_field_setting( $field, array( 100 | 'label' => __('Hide','acf-auto_generated_value'), 101 | 'instructions' => __('Hide the field from the user','acf-auto_generated_value'), 102 | 'type' => 'radio', 103 | 'layout' => 'horizontal', 104 | 'name' => 'hide', 105 | 'choices' => array('yes' => 'Yes', 'no' => 'No'), 106 | )); 107 | 108 | } 109 | 110 | 111 | 112 | /* 113 | * render_field() 114 | * 115 | * Create the HTML interface for your field 116 | * 117 | * @param $field (array) the $field being rendered 118 | * 119 | * @type action 120 | * @since 3.6 121 | * @date 23/01/13 122 | * 123 | * @param $field (array) the $field being edited 124 | * @return n/a 125 | */ 126 | 127 | function render_field( $field ) { 128 | 129 | 130 | /* 131 | * Review the data of $field. 132 | * This will show what data is available 133 | */ 134 | 135 | /* 136 | * Create a simple text input using the 'font_size' setting. 137 | */ 138 | ?> 139 |
140 | Edit 141 |
142 | 12 ) { 388 | 389 | // format the value 390 | // $value = 'something'; 391 | 392 | } 393 | 394 | 395 | // return 396 | return $value; 397 | } 398 | 399 | */ 400 | 401 | 402 | /* 403 | * validate_value() 404 | * 405 | * This filter is used to perform validation on the value prior to saving. 406 | * All values are validated regardless of the field's required setting. This allows you to validate and return 407 | * messages to the user if the value is not correct 408 | * 409 | * @type filter 410 | * @date 11/02/2014 411 | * @since 5.0.0 412 | * 413 | * @param $valid (boolean) validation status based on the value and the field's required setting 414 | * @param $value (mixed) the $_POST value 415 | * @param $field (array) the field array holding all the field options 416 | * @param $input (string) the corresponding input name for $_POST value 417 | * @return $valid 418 | */ 419 | 420 | /* 421 | 422 | function validate_value( $valid, $value, $field, $input ){ 423 | 424 | // Basic usage 425 | if( $value < $field['custom_minimum_setting'] ) 426 | { 427 | $valid = false; 428 | } 429 | 430 | 431 | // Advanced usage 432 | if( $value < $field['custom_minimum_setting'] ) 433 | { 434 | $valid = __('The value is too little!','acf-auto_generated_value'), 435 | } 436 | 437 | 438 | // return 439 | return $valid; 440 | 441 | } 442 | 443 | */ 444 | 445 | 446 | /* 447 | * delete_value() 448 | * 449 | * This action is fired after a value has been deleted from the db. 450 | * Please note that saving a blank value is treated as an update, not a delete 451 | * 452 | * @type action 453 | * @date 6/03/2014 454 | * @since 5.0.0 455 | * 456 | * @param $post_id (mixed) the $post_id from which the value was deleted 457 | * @param $key (string) the $meta_key which the value was deleted 458 | * @return n/a 459 | */ 460 | 461 | /* 462 | 463 | function delete_value( $post_id, $key ) { 464 | 465 | 466 | 467 | } 468 | 469 | */ 470 | 471 | 472 | /* 473 | * load_field() 474 | * 475 | * This filter is applied to the $field after it is loaded from the database 476 | * 477 | * @type filter 478 | * @date 23/01/2013 479 | * @since 3.6.0 480 | * 481 | * @param $field (array) the field array holding all the field options 482 | * @return $field 483 | */ 484 | 485 | /* 486 | 487 | function load_field( $field ) { 488 | 489 | return $field; 490 | 491 | } 492 | 493 | */ 494 | 495 | 496 | /* 497 | * update_field() 498 | * 499 | * This filter is applied to the $field before it is saved to the database 500 | * 501 | * @type filter 502 | * @date 23/01/2013 503 | * @since 3.6.0 504 | * 505 | * @param $field (array) the field array holding all the field options 506 | * @return $field 507 | */ 508 | 509 | /* 510 | 511 | function update_field( $field ) { 512 | 513 | return $field; 514 | 515 | } 516 | 517 | */ 518 | 519 | 520 | /* 521 | * delete_field() 522 | * 523 | * This action is fired after a field is deleted from the database 524 | * 525 | * @type action 526 | * @date 11/02/2014 527 | * @since 5.0.0 528 | * 529 | * @param $field (array) the field array holding all the field options 530 | * @return n/a 531 | */ 532 | 533 | /* 534 | 535 | function delete_field( $field ) { 536 | 537 | 538 | 539 | } 540 | 541 | */ 542 | 543 | 544 | } 545 | 546 | 547 | // create field 548 | new acf_field_auto_generated_value(); 549 | 550 | ?> 551 | -------------------------------------------------------------------------------- /acf-auto_generated_value.php: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /css/README.md: -------------------------------------------------------------------------------- 1 | # CSS directory 2 | 3 | Use this directory to store CSS files. 4 | 5 | This directory can be removed if not used. 6 | -------------------------------------------------------------------------------- /css/input.css: -------------------------------------------------------------------------------- 1 | .auto-generated-value-input-wrapper{ 2 | position: relative; 3 | } 4 | .auto-generated-value-input-wrapper .edit-link{ 5 | position: absolute; 6 | right:5px; 7 | top:5px; 8 | color: #bbb; 9 | cursor:pointer; 10 | } -------------------------------------------------------------------------------- /images/README.md: -------------------------------------------------------------------------------- 1 | # Images directory 2 | 3 | Use this directory to store images. 4 | 5 | This directory can be removed if not used. 6 | -------------------------------------------------------------------------------- /js/README.md: -------------------------------------------------------------------------------- 1 | # JS directory 2 | 3 | Use this directory to store JS files. 4 | 5 | This directory can be removed if not used. 6 | -------------------------------------------------------------------------------- /js/input.js: -------------------------------------------------------------------------------- 1 | (function($){ 2 | 3 | 4 | function initialize_field( $el ) { 5 | var $input = $el.find('.auto-generated-value-input'); 6 | 7 | if($input.data('hide') === 'yes' && $el.is('tr')){ 8 | $el.hide(); 9 | } 10 | 11 | 12 | // Check if value is set 13 | if( ! $input.val() ){ 14 | // If not, get all existing values to make sure the new one is unique 15 | var $repeater = $el.parents('[data-type=repeater]'); 16 | 17 | var values = []; 18 | $repeater.find('.auto-generated-value-input').each(function(){ 19 | if($(this).val()){ 20 | values.push($(this).val()); 21 | } 22 | }); 23 | // Start from zero 24 | var count = 0; 25 | var value = $input.data('prefix') + count; 26 | // Generate new values until we get one that doesn't exist 27 | console.log(values); 28 | while(values.indexOf(value) > -1){ 29 | count++; 30 | value = $input.data('prefix') + count; 31 | } 32 | $input.val(value); 33 | } 34 | 35 | // Handle edit button 36 | $el.find('.auto-generated-value-input-wrapper .edit-link').click(function(){ 37 | if(confirm('Warning: Editing this field might affect existing entries for this form and could cause lost data.')){ 38 | $input.removeAttr('readonly'); 39 | $(this).hide(); 40 | $input.focus().select(); 41 | } 42 | 43 | }); 44 | } 45 | 46 | 47 | if( typeof acf.add_action !== 'undefined' ) { 48 | 49 | /* 50 | * ready append (ACF5) 51 | * 52 | * These are 2 events which are fired during the page load 53 | * ready = on page load similar to $(document).ready() 54 | * append = on new DOM elements appended via repeater field 55 | * 56 | * @type event 57 | * @date 20/07/13 58 | * 59 | * @param $el (jQuery selection) the jQuery element which contains the ACF fields 60 | * @return n/a 61 | */ 62 | 63 | acf.add_action('ready append', function( $el ){ 64 | 65 | // search $el for fields of type 'auto_generated_value' 66 | acf.get_fields({ type : 'auto_generated_value'}, $el).each(function(){ 67 | 68 | initialize_field( $(this) ); 69 | 70 | }); 71 | 72 | }); 73 | 74 | 75 | } else { 76 | 77 | 78 | /* 79 | * acf/setup_fields (ACF4) 80 | * 81 | * This event is triggered when ACF adds any new elements to the DOM. 82 | * 83 | * @type function 84 | * @since 1.0.0 85 | * @date 01/01/12 86 | * 87 | * @param event e: an event object. This can be ignored 88 | * @param Element postbox: An element which contains the new HTML 89 | * 90 | * @return n/a 91 | */ 92 | 93 | $(document).live('acf/setup_fields', function(e, postbox){ 94 | 95 | $(postbox).find('.field[data-field_type="auto_generated_value"]').each(function(){ 96 | 97 | initialize_field( $(this) ); 98 | 99 | }); 100 | 101 | }); 102 | 103 | 104 | } 105 | 106 | 107 | })(jQuery); 108 | -------------------------------------------------------------------------------- /lang/README.md: -------------------------------------------------------------------------------- 1 | # Translations directory 2 | 3 | Use this directory to store .po and .mo files. 4 | 5 | This directory can be removed if not used. 6 | -------------------------------------------------------------------------------- /readme.txt: -------------------------------------------------------------------------------- 1 | === Advanced Custom Fields: Auto-generated value Field === 2 | Contributors: Kindly 3 | Tags: hidden,auto,unique,repeater 4 | Requires at least: 3.5 5 | Tested up to: 3.8.1 6 | Stable tag: trunk 7 | License: GPLv2 or later 8 | License URI: http://www.gnu.org/licenses/gpl-2.0.html 9 | 10 | Creates a hidden/readonly field in a repeater that gets a unique key, that won't change once it is saved. Ideal for user-generated forms. 11 | 12 | == Creates a hidden/readonly field in a repeater that gets a unique key, that won't change once it is saved. Ideal for user-generated forms. == 13 | 14 | EXTENDED_Creates a hidden/readonly field in a repeater that gets a unique key, that won't change once it is saved. Ideal for user-generated forms. 15 | 16 | = Compatibility = 17 | 18 | This ACF field type is compatible with: 19 | * ACF 5 20 | * ACF 4 21 | 22 | == Installation == 23 | 24 | 1. Copy the `acf-auto_generated_value` folder into your `wp-content/plugins` folder 25 | 2. Activate the Auto-generated value plugin via the plugins admin page 26 | 3. Create a new field via ACF and select the Auto-generated value type 27 | 4. Please refer to the Creates a hidden/readonly field in a repeater that gets a unique key, that won't change once it is saved. Ideal for user-generated forms. for more info regarding the field type settings 28 | 29 | == Changelog == 30 | 31 | = 1.0.0 = 32 | * Initial Release. --------------------------------------------------------------------------------