├── .gitignore ├── README.md ├── acf-random-string-field-v5.php ├── acf-random-string-field.php ├── css └── input.css ├── js └── input.js └── 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 | ----------------------- 2 | 3 | # ACF Random String Field 4 | 5 | ### Description 6 | 7 | Generates a random string using a pool of alphanumeric and special characters. 8 | 9 | ### Compatibility 10 | 11 | This ACF field type is compatible with: 12 | * ACF 5 13 | 14 | ### Installation 15 | 16 | 1. Copy the `acf-random-string-field` folder into your `wp-content/plugins` folder 17 | 2. Activate the Random String Field plugin via the plugins admin page 18 | 3. Create a new field via ACF and select the Random String type 19 | 4. Please refer to the description for more info regarding the field type settings 20 | 21 | ### Changelog 22 | 23 | **1.1.0** 24 | * Feature: Added 'Alphanumeric only' option. 25 | * Tweak: Added more characters to possible character pool. 26 | 27 | **1.0.0** 28 | * Initial release. 29 | 30 | ----------------------- 31 | -------------------------------------------------------------------------------- /acf-random-string-field-v5.php: -------------------------------------------------------------------------------- 1 | name = 'acf-random-string'; 26 | 27 | 28 | /* 29 | * label (string) Multiple words, can include spaces, visible when selecting a field type 30 | */ 31 | 32 | $this->label = __('Random String', 'acf-random-string'); 33 | 34 | 35 | /* 36 | * category (string) basic | content | choice | relational | jquery | layout | CUSTOM GROUP NAME 37 | */ 38 | 39 | $this->category = 'content'; 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 | $random_string = implode('', array_map(function () { return chr(rand(0, 1) ? rand(48, 57) : rand(97, 122)); }, range(0, 9))); 47 | 48 | $this->defaults = array( 49 | 'length' => 10, 50 | 'alphanumeric' => false 51 | ); 52 | 53 | 54 | /* 55 | * l10n (array) Array of strings that are used in JavaScript. This allows JS strings to be translated in PHP and loaded via: 56 | * var message = acf._e('FIELD_NAME', 'error'); 57 | */ 58 | 59 | $this->l10n = array( 60 | 'error' => __('Error! Please enter a higher value', 'acf-random-string'), 61 | ); 62 | 63 | 64 | // do not delete! 65 | parent::__construct(); 66 | 67 | } 68 | 69 | 70 | /* 71 | * render_field_settings() 72 | * 73 | * Create extra settings for your field. These are visible when editing a field 74 | * 75 | * @type action 76 | * @since 3.6 77 | * @date 23/01/13 78 | * 79 | * @param $field (array) the $field being edited 80 | * @return n/a 81 | */ 82 | 83 | function render_field_settings( $field ) { 84 | 85 | /* 86 | * acf_render_field_setting 87 | * 88 | * This function will create a setting for your field. Simply pass the $field parameter and an array of field settings. 89 | * The array of settings does not require a `value` or `prefix`; These settings are found from the $field array. 90 | * 91 | * More than one setting can be added by copy/paste the above code. 92 | * Please note that you must also have a matching $defaults value for the field name (font_size) 93 | */ 94 | 95 | acf_render_field_setting( $field, array( 96 | 'label' => __('Length of string','acf-random-string'), 97 | 'instructions' => __('Minimum of 1','acf-random-string'), 98 | 'name' => 'length' 99 | )); 100 | 101 | acf_render_field_setting( $field, array( 102 | 'label' => __('Alphanumeric only','acf-random-string'), 103 | 'instructions' => __('','acf-random-string'), 104 | 'name' => 'alphanumeric', 105 | 'type' => 'checkbox', 106 | 'choices' => array( 107 | '1' => 'Yes' 108 | ) 109 | )); 110 | 111 | } 112 | 113 | 114 | 115 | /* 116 | * render_field() 117 | * 118 | * Create the HTML interface for your field 119 | * 120 | * @param $field (array) the $field being rendered 121 | * 122 | * @type action 123 | * @since 3.6 124 | * @date 23/01/13 125 | * 126 | * @param $field (array) the $field being edited 127 | * @return n/a 128 | */ 129 | 130 | function render_field( $field ) { 131 | 132 | 133 | /* 134 | * Review the data of $field. 135 | * This will show what data is available 136 | */ 137 | 138 | /* 139 | echo '
';
140 | 			print_r( $field );
141 | 		echo '
'; 142 | */ 143 | 144 | 145 | /* 146 | * Create a simple text input using the 'font_size' setting. 147 | */ 148 | 149 | ?> 150 |
151 | 152 | 153 |
154 |
155 | 12 ) { 399 | 400 | // format the value 401 | // $value = 'something'; 402 | 403 | } 404 | 405 | 406 | // return 407 | return $value; 408 | } 409 | 410 | */ 411 | 412 | 413 | /* 414 | * validate_value() 415 | * 416 | * This filter is used to perform validation on the value prior to saving. 417 | * All values are validated regardless of the field's required setting. This allows you to validate and return 418 | * messages to the user if the value is not correct 419 | * 420 | * @type filter 421 | * @date 11/02/2014 422 | * @since 5.0.0 423 | * 424 | * @param $valid (boolean) validation status based on the value and the field's required setting 425 | * @param $value (mixed) the $_POST value 426 | * @param $field (array) the field array holding all the field options 427 | * @param $input (string) the corresponding input name for $_POST value 428 | * @return $valid 429 | */ 430 | 431 | /* 432 | 433 | function validate_value( $valid, $value, $field, $input ){ 434 | 435 | // Basic usage 436 | if( $value < $field['custom_minimum_setting'] ) 437 | { 438 | $valid = false; 439 | } 440 | 441 | 442 | // Advanced usage 443 | if( $value < $field['custom_minimum_setting'] ) 444 | { 445 | $valid = __('The value is too little!','acf-FIELD_NAME'), 446 | } 447 | 448 | 449 | // return 450 | return $valid; 451 | 452 | } 453 | 454 | */ 455 | 456 | 457 | /* 458 | * delete_value() 459 | * 460 | * This action is fired after a value has been deleted from the db. 461 | * Please note that saving a blank value is treated as an update, not a delete 462 | * 463 | * @type action 464 | * @date 6/03/2014 465 | * @since 5.0.0 466 | * 467 | * @param $post_id (mixed) the $post_id from which the value was deleted 468 | * @param $key (string) the $meta_key which the value was deleted 469 | * @return n/a 470 | */ 471 | 472 | /* 473 | 474 | function delete_value( $post_id, $key ) { 475 | 476 | 477 | 478 | } 479 | 480 | */ 481 | 482 | 483 | /* 484 | * load_field() 485 | * 486 | * This filter is applied to the $field after it is loaded from the database 487 | * 488 | * @type filter 489 | * @date 23/01/2013 490 | * @since 3.6.0 491 | * 492 | * @param $field (array) the field array holding all the field options 493 | * @return $field 494 | */ 495 | 496 | /* 497 | 498 | function load_field( $field ) { 499 | 500 | return $field; 501 | 502 | } 503 | 504 | */ 505 | 506 | 507 | /* 508 | * update_field() 509 | * 510 | * This filter is applied to the $field before it is saved to the database 511 | * 512 | * @type filter 513 | * @date 23/01/2013 514 | * @since 3.6.0 515 | * 516 | * @param $field (array) the field array holding all the field options 517 | * @return $field 518 | */ 519 | 520 | /* 521 | 522 | function update_field( $field ) { 523 | 524 | return $field; 525 | 526 | } 527 | 528 | */ 529 | 530 | 531 | /* 532 | * delete_field() 533 | * 534 | * This action is fired after a field is deleted from the database 535 | * 536 | * @type action 537 | * @date 11/02/2014 538 | * @since 5.0.0 539 | * 540 | * @param $field (array) the field array holding all the field options 541 | * @return n/a 542 | */ 543 | 544 | /* 545 | 546 | function delete_field( $field ) { 547 | 548 | 549 | 550 | } 551 | 552 | */ 553 | 554 | 555 | } 556 | 557 | 558 | // create field 559 | new acf_field_random_string(); 560 | 561 | ?> 562 | -------------------------------------------------------------------------------- /acf-random-string-field.php: -------------------------------------------------------------------------------- 1 | 51 | -------------------------------------------------------------------------------- /css/input.css: -------------------------------------------------------------------------------- 1 | .acf-field .acf-random-string-field-wrap .acf-random-string-field-input{ 2 | float: left; 3 | width: 78%; 4 | margin-right: 2%; 5 | } 6 | 7 | .acf-field .acf-random-string-field-wrap .acf-random-string-field-button{ 8 | float: left; 9 | width: 20%; 10 | } -------------------------------------------------------------------------------- /js/input.js: -------------------------------------------------------------------------------- 1 | (function($){ 2 | 3 | 4 | function initialize_field( $el ) { 5 | 6 | $('.acf-random-string-field-button').on('click', function(e){ 7 | 8 | var string_length = $(this).attr('data-length'); 9 | var alphanumeric = $(this).attr('data-alphanumeric'); 10 | 11 | var random_string = ""; 12 | var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789#%!&*$@"; 13 | 14 | if( alphanumeric ){ 15 | possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; 16 | } 17 | 18 | for( var i=0; i < string_length; i++ ) 19 | random_string += possible.charAt(Math.floor(Math.random() * possible.length)); 20 | 21 | $(this).siblings('.acf-random-string-field-input').val(random_string); 22 | 23 | e.preventDefault(); 24 | return false; 25 | }); 26 | 27 | } 28 | 29 | 30 | if( typeof acf.add_action !== 'undefined' ) { 31 | 32 | /* 33 | * ready append (ACF5) 34 | * 35 | * These are 2 events which are fired during the page load 36 | * ready = on page load similar to $(document).ready() 37 | * append = on new DOM elements appended via repeater field 38 | * 39 | * @type event 40 | * @date 20/07/13 41 | * 42 | * @param $el (jQuery selection) the jQuery element which contains the ACF fields 43 | * @return n/a 44 | */ 45 | 46 | acf.add_action('ready append', function( $el ){ 47 | 48 | // search $el for fields of type 'FIELD_NAME' 49 | acf.get_fields({ type : 'acf-random-string'}, $el).each(function(){ 50 | 51 | initialize_field( $(this) ); 52 | 53 | }); 54 | 55 | }); 56 | 57 | 58 | } else { 59 | 60 | 61 | /* 62 | * acf/setup_fields (ACF4) 63 | * 64 | * This event is triggered when ACF adds any new elements to the DOM. 65 | * 66 | * @type function 67 | * @since 1.0.0 68 | * @date 01/01/12 69 | * 70 | * @param event e: an event object. This can be ignored 71 | * @param Element postbox: An element which contains the new HTML 72 | * 73 | * @return n/a 74 | */ 75 | 76 | $(document).on('acf/setup_fields', function(e, postbox){ 77 | 78 | $(postbox).find('.field[data-field_type="acf-random-string"]').each(function(){ 79 | 80 | initialize_field( $(this) ); 81 | 82 | }); 83 | 84 | }); 85 | 86 | 87 | } 88 | 89 | 90 | })(jQuery); 91 | -------------------------------------------------------------------------------- /readme.txt: -------------------------------------------------------------------------------- 1 | === Advanced Custom Fields: Random String Field === 2 | Contributors: luke chapman 3 | Tags: acf random string field 4 | Requires at least: 5.2.1 5 | Tested up to: 5.2.1 6 | Stable tag: trunk 7 | License: GPLv2 or later 8 | License URI: http://www.gnu.org/licenses/gpl-2.0.html 9 | 10 | == Description == 11 | 12 | Generates a random string using a pool of alphanumeric and special characters. 13 | 14 | = Compatibility = 15 | 16 | This ACF field type is compatible with: 17 | * ACF 5 18 | 19 | == Installation == 20 | 21 | 1. Copy the `acf-random-string-field` folder into your `wp-content/plugins` folder 22 | 2. Activate the Random String Field plugin via the plugins admin page 23 | 3. Create a new field via ACF and select the Random String type 24 | 25 | == Changelog == 26 | = 1.1.0 = 27 | * Feature: Added 'Alphanumeric only' option. 28 | * Fix: Added more characters to possible character pool. 29 | = 1.0.0 = 30 | * Initial release. --------------------------------------------------------------------------------