0

Textarea is on an options page, is it possible to sort the values within that textarea by alpha? I looked in the Codex but do not see any mention of sorting these values.

  • 2
    A textarea is a single value. The only way to sort that is to parse it somehow, with Regex perhaps? – jdm2112 Nov 19 '15 at 3:07
  • what if the value's had been run through a foreach() statement and pushed into a select metabox using a printf() statement? – John Lucey Nov 19 '15 at 3:10
  • 1
    Post your code please. – s_ha_dum Nov 19 '15 at 4:31
1
$values = get_option( 'option_name')
// Create array from string
$values_array = explode( PHP_EOL, $values );
sort( $values_array );
// Create string from sorted array
$sorted_values = implode( PHP_EOL, $values_array );

Read more about the sort function

  • @ville6000-this is exactly what I was wondering, I started reading about sorting arrays. In my head I felt as though the values coming from the options page textarea could be stored in an array. So I started reading about sorting an array. I just tested this and found that to accomplish what I am trying to accomplish I just drop the $sorted_values line, no need to string them back together as this is a select box. So I just grabbed the $values Array and stuffed the select box with it and it works perfect. – John Lucey Nov 19 '15 at 13:07

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.