name ) ) return ''; $validation_error = wpcf7_get_validation_error( $tag->name ); $class = wpcf7_form_controls_class( $tag->type ); $class .= ' wpcf7-validates-as-number'; if ( $validation_error ) $class .= ' wpcf7-not-valid'; $atts = array(); $atts['class'] = $tag->get_class_option( $class ); $atts['id'] = $tag->get_id_option(); $atts['tabindex'] = $tag->get_option( 'tabindex', 'int', true ); $atts['min'] = $tag->get_option( 'min', 'signed_int', true ); $atts['max'] = $tag->get_option( 'max', 'signed_int', true ); $atts['step'] = $tag->get_option( 'step', 'int', true ); if ( $tag->has_option( 'readonly' ) ) $atts['readonly'] = 'readonly'; if ( $tag->is_required() ) $atts['aria-required'] = 'true'; $atts['aria-invalid'] = $validation_error ? 'true' : 'false'; $value = (string) reset( $tag->values ); if ( $tag->has_option( 'placeholder' ) || $tag->has_option( 'watermark' ) ) { $atts['placeholder'] = $value; $value = ''; } $value = $tag->get_default_option( $value ); $value = wpcf7_get_hangover( $tag->name, $value ); $atts['value'] = $value; if ( wpcf7_support_html5() ) { $atts['type'] = $tag->basetype; } else { $atts['type'] = 'text'; } $atts['name'] = $tag->name; $atts = wpcf7_format_atts( $atts ); $html = sprintf( '%3$s', sanitize_html_class( $tag->name ), $atts, $validation_error ); return $html; } /* Validation filter */ add_filter( 'wpcf7_validate_number', 'wpcf7_number_validation_filter', 10, 2 ); add_filter( 'wpcf7_validate_number*', 'wpcf7_number_validation_filter', 10, 2 ); add_filter( 'wpcf7_validate_range', 'wpcf7_number_validation_filter', 10, 2 ); add_filter( 'wpcf7_validate_range*', 'wpcf7_number_validation_filter', 10, 2 ); function wpcf7_number_validation_filter( $result, $tag ) { $tag = new WPCF7_Shortcode( $tag ); $name = $tag->name; $value = isset( $_POST[$name] ) ? trim( strtr( (string) $_POST[$name], "\n", " " ) ) : ''; $min = $tag->get_option( 'min', 'signed_int', true ); $max = $tag->get_option( 'max', 'signed_int', true ); if ( $tag->is_required() && '' == $value ) { $result->invalidate( $tag, wpcf7_get_message( 'invalid_required' ) ); } elseif ( '' != $value && ! wpcf7_is_number( $value ) ) { $result->invalidate( $tag, wpcf7_get_message( 'invalid_number' ) ); } elseif ( '' != $value && '' != $min && (float) $value < (float) $min ) { $result->invalidate( $tag, wpcf7_get_message( 'number_too_small' ) ); } elseif ( '' != $value && '' != $max && (float) $max < (float) $value ) { $result->invalidate( $tag, wpcf7_get_message( 'number_too_large' ) ); } return $result; } /* Messages */ add_filter( 'wpcf7_messages', 'wpcf7_number_messages' ); function wpcf7_number_messages( $messages ) { return array_merge( $messages, array( 'invalid_number' => array( 'description' => __( "Number format that the sender entered is invalid", 'contact-form-7' ), 'default' => __( "The number format is invalid.", 'contact-form-7' ) ), 'number_too_small' => array( 'description' => __( "Number is smaller than minimum limit", 'contact-form-7' ), 'default' => __( "The number is smaller than the minimum allowed.", 'contact-form-7' ) ), 'number_too_large' => array( 'description' => __( "Number is larger than maximum limit", 'contact-form-7' ), 'default' => __( "The number is larger than the maximum allowed.", 'contact-form-7' ) ) ) ); } /* Tag generator */ add_action( 'wpcf7_admin_init', 'wpcf7_add_tag_generator_number', 18 ); function wpcf7_add_tag_generator_number() { $tag_generator = WPCF7_TagGenerator::get_instance(); $tag_generator->add( 'number', __( 'number', 'contact-form-7' ), 'wpcf7_tag_generator_number' ); } function wpcf7_tag_generator_number( $contact_form, $args = '' ) { $args = wp_parse_args( $args, array() ); $type = 'number'; $description = __( "Generate a form-tag for a field for numeric value input. For more details, see %s.", 'contact-form-7' ); $desc_link = wpcf7_link( __( 'http://contactform7.com/number-fields/', 'contact-form-7' ), __( 'Number Fields', 'contact-form-7' ) ); ?>