PluginProbe
User Profile Builder – Beautiful User Registration Forms, User Profiles & User Role Editor / 3.9.8
User Profile Builder – Beautiful User Registration Forms, User Profiles & User Role Editor v3.9.8
4.0.3 4.0.2 4.0.1 4.0.0 3.16.6 3.16.5 3.16.4 3.16.3 3.16.2 3.16.1 3.16.0 3.15.9 3.9.9 3.9.5 3.9.6 3.9.7 3.9.8 1.1.7 1.1.8 1.1.9 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 All 341 releases
profile-builder / add-ons-free / maximum-character-length / maximum-character-length.php

maximum-character-length.php in User Profile Builder – Beautiful User Registration Forms, User Profiles & User Role Editor 3.9.8, at add-ons-free/maximum-character-length/maximum-character-length.php

76 lines 2.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /*
3 Description: Extends the functionality of Profile Builder by allowing you to set a maximum character length for the custom input and textarea fields.
4 */
5
6 /*
7 * Function that enqueues the necessary scripts
8 *
9 * @since v.1.0.0
10 */
11 function wppb_mcl_scripts($hook) {
12 if ( $hook == 'profile-builder_page_manage-fields' ) {
13 wp_enqueue_script( 'wppb-max-char-length', plugin_dir_url(__FILE__) . 'assets/js/main.js', array( 'jquery', 'wppb-manage-fields-live-change' ) );
14 }
15 }
16 add_action( 'admin_enqueue_scripts', 'wppb_mcl_scripts' );
17
18 /*
19 * Function that adds maximum character length to the field properties in manage fields
20 *
21 * @since v.1.0.0
22 *
23 * @param array $fields - The current field properties
24 *
25 * @return array - The field properties that now include the maximum character length property
26 */
27 function wppb_mcl_manage_field( $fields ) {
28 $max_length_manage_field = array( 'type' => 'text', 'slug' => 'maximum-character-length', 'title' => __( 'Maximum Character Length', 'profile-builder' ), 'description' => __( "Specify the maximum number of characters a user can type in this field", 'profile-builder' ) );
29 array_push( $fields, $max_length_manage_field );
30
31 return $fields;
32 }
33 add_filter( 'wppb_manage_fields', 'wppb_mcl_manage_field' );
34
35 /*
36 * Function that checks to see if the maximum character length that the user entered is
37 * a number, we don't want random strings
38 *
39 * @since v.1.0.0
40 *
41 * @param string $message - The error messages that will be displayed to the user
42 * @param array $posted_values - The information the user has entered for the field
43 *
44 * @return string
45 */
46 function wppb_mcl_check_max_character_length( $message, $posted_values ) {
47
48 if ( isset( $posted_values['maximum-character-length'] ) && !is_numeric( $posted_values['maximum-character-length'] ) && !empty( $posted_values['maximum-character-length'] ) )
49 $message .= __( "The entered character number is not numerical\n", 'profile-builder' );
50
51 return $message;
52 }
53 add_filter( 'wppb_check_extra_manage_fields', 'wppb_mcl_check_max_character_length', 10, 2 );
54
55 /*
56 * Function that changes the default maximum character length with the one
57 * the user has set
58 *
59 * @since v.1.0.0
60 *
61 * @param int $default_value
62 * @param array $field
63 *
64 * @return int
65 */
66 function wppb_mcl_set_max_character_length( $default_value, $field = '' ) {
67 $output = $default_value;
68
69 if( isset( $field['maximum-character-length'] ) && !empty( $field['maximum-character-length'] ) ) {
70 $output = (int)$field['maximum-character-length'];
71 }
72
73 return $output;
74 }
75 add_filter( 'wppb_maximum_character_length', 'wppb_mcl_set_max_character_length', 10 , 2);
76