I am using the Advanced Custom Fields plugin, which is very good.

I am trying to take the output of one of the fields I created and give it the "permalink treatment", that is strip out non-alphanumeric characters like slashes, convert uppercase to lowercase, and convert spaces to dashes.

Specifically I am wanting to do this with the checkbox field. I want to do this, because I want to use the outputted values as css class names.

Its output is called by this bit of PHP (modified slightly from the documented use, see example #4):


$values = get_field('field_name');

foreach($values as $value)
    echo ' ' . $value;



Which returns all of the checkbox values (5 - 10 in this case) separated by a space. I successfully got the values showing up as class names, but now I need a way to format them to actually work as class names.

How could I tweak this to work? I found this bit that seems to work for a sinlge string:

function stripJunk($string){    
    $string = str_replace(" ","-", trim($string));         
    $string = preg_replace("/[^a-zA-Z0-9-]/","", $string);
    $string = strtolower($string);
    return $string;}

but in my case it's an array, and I don't know how to work with that. I'm pretty worthless when it comes to PHP, except for being able to tweak things a little bit without breaking it.


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: ✂ ✉ ✔✽ ✾ ✿ ❀ ❉ ❤ ❦ ☂ ☔ ☎ ☘ ♫ ♻ ⚥


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. :)

  • Thank you. I figured WordPress would already have something in place for that. I still can't figure out how to use it in my context, though. If only I could jack in for a few hours like the Matrix, wake up and say: "I know PHP." Apr 14 '12 at 20:01
  • Thank you so much for taking the time to do this. I get a server error when I include that function. I changed 'field_name' to match my actual field name. I'm not sure what that first $field_name variable is all about. Apr 14 '12 at 23:56
  • I'll see what I can do to get this working. Apr 15 '12 at 0:05
  • Ah, I had a bug in the function. That happens when I cannot work on my server. Fixed (I hope). :)
    – fuxia
    Apr 15 '12 at 0:14
  • Still get the server error whenever that bit is included. I really appreciate the effort and I wish I knew enough to make it work. It's all good, though. If you have time to look into it more, great, if not, it's not a make or break issue. Apr 15 '12 at 5:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.