You could use sanitize_html_class()
or sanitize_title_with_dashes()
. Both functions do almost the same. But note that much more characters a valid in CSS class names.
Examples: ✂ ✉ ✔✽ ✾ ✿ ❀ ❉ ❤ ❦ ☂ ☔ ☎ ☘ ♫ ♻ ⚥
Update
I recommend to encapsulate the code in a prefixed function to avoid collisions with poorly written plugin using the same names as you do.
Example (not tested):
function wpse_48986_custom_fields_to_classes( $field_name )
{
$values = get_field( $field_name );
if ( is_string( $values ) )
{
return trim( sanitize_html_class( $values ) );
}
if ( is_array( $values ) )
{
// Prepare the array values.
$values = array_map( 'sanitize_html_class', $values );
$values = array_map( 'trim', $values );
return implode( ' ', $values );
}
// We didn't get a string or an array.
return '';
}
And when you need the CSS classes somewhere just write:
print wpse_48986_custom_fields_to_classes( 'field_name' );
I couldn’t test the code code: I don’t have the plugin installed, and my main computer is broken, so there may be errors. Feel free to edit this post and fix the broken code. 🙂