Normalises a mixed tag input into a clean array of valid integer tag IDs. Accepts a single value or an array containing any mix of tag IDs, tag slugs, tag names, or Tag objects. Tags that cannot be resolved are silently dropped. Use this when accepting tag input from external sources such as API requests or form submissions.
Parameters
| Parameter | Type | Description |
|---|---|---|
$maybe_tags | mixed | A single tag identifier or an array of tag identifiers. Accepts int IDs, string names/slugs, or Tag objects. |
Return value
array β An array of validated integer tag IDs.
Usage
$tag_ids = \Groundhogg\validate_tags( [ 1, 'newsletter', 'vip-customer' ] );
$contact->apply_tag( $tag_ids );
Example
Here’s a pseudo code example for a hypothetical REST API endpoint that accepts tags.
add_action( 'rest_api_init', function() {
register_rest_route( 'my-plugin/v1', '/apply-tags', [
'methods' => 'POST',
'callback' => 'my_plugin_apply_tags_endpoint',
'permission_callback' => '__return_true',
] );
} );
/**
* Apply tags to a contact from a REST API request.
*
* @param \WP_REST_Request $request
*
* @return \WP_REST_Response
*/
function my_plugin_apply_tags_endpoint( $request ) {
$email = sanitize_email( $request->get_param( 'email' ) );
$contact = \Groundhogg\get_contactdata( $email );
if ( ! $contact ) {
return new \WP_REST_Response( [ 'error' => 'Contact not found.' ], 404 );
}
$tag_ids = \Groundhogg\validate_tags( $request->get_param( 'tags' ) ?? [] );
if ( empty( $tag_ids ) ) {
return new \WP_REST_Response( [ 'error' => 'No valid tags provided.' ], 400 );
}
$contact->apply_tag( $tag_ids );
return new \WP_REST_Response( [ 'applied' => $tag_ids ], 200 );
}
Was this helpful?
Let us know if this document answered your question. Thatβs the only way we can improve.
