Documentation » Filter Hooks

groundhogg/sanitize_object_meta

Published · Last updated

Filters a meta value before it is saved to the database for any Groundhogg object. Use this to sanitise, validate, or transform custom meta values written by your extension.

apply_filters( 'groundhogg/sanitize_object_meta', $value, $key, $object_type, $original_value );

You will receive the sanitised value, the meta key string, the object type string (e.g. 'contact'), and the original unsanitised value.

Example

add_filter( 'groundhogg/sanitize_object_meta', 'my_extension_sanitize_meta', 10, 4 );

/**
 * Enforce a maximum value for the loyalty_points meta key.
 *
 * @param mixed  $value
 * @param string $key
 * @param string $object_type
 * @param mixed  $original_value
 *
 * @return mixed
 */
function my_extension_sanitize_meta( $value, $key, $object_type, $original_value ) {
    if ( $key === 'loyalty_points' && $object_type === 'contact' ) {
        return min( absint( $value ), 10000 );
    }

    return $value;
}