The Data Types API is used by WordPoints to provide a common interface for interacting with values of various types.
The existing Data Types include:
text
— A string of text (like the content of a post).integer
— An integer number value (like the ID of a post).decimal_number
— Floats, numbers composed entirely of integers and an optional decimal (like the price of a product). (Added in 2.3.0)
The following Data Types are reserved, but not yet implemented:
mysql_datetime
— Date-time strings in MySQL’sDATETIME
format (YYYY-MM-DD HH:MM:SS
).unix_timestamp
— Integer representing the number of seconds since the Unix Epoch.
Adding a new data type is fairly simple, all you have to do is extend the WordPoints_Data_Type
class, and define the validate_value()
method to check if a value is of that data type. The class for the integer
data type looks like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | /** * A handler for integer data. * * @since 2.1.0 */ class WordPoints_Data_Type_Integer extends WordPoints_Data_Type { /** * @since 2.1.0 */ public function validate_value( $value ) { wordpoints_int( $value ); if ( false === $value ) { return new WP_Error( 'not_integer' // translators: Form field name. , __( '%s must be an integer.', 'wordpoints' ) ); } return $value; } } |
As you can see, all that the validate_value()
method needs to do is see if a passed in value is the correct type, and if it isn’t to return a WP_Error
object containing an error message. The error message should contain a placeholder, which will be filled in with the form field name by the code that is validating the value.