I found a way based in Sorting options elements alphabetically using jQuery
Steps:
1. Install and activate CSS & JavaScript Toolbox WP plugin
2. Go to plugin’s admin page at yourwebsite.com/wp-admin/admin.php?page=cjtoolbox
3. Add a “+ New Code Block” and name it as you wish
4. Past the following code:
<script type="text/javascript">
jQuery(document).ready(function() {
function sortSelectOptions(selector, skip_first) {
var options = (skip_first) ? jQuery(selector + ' option:not(:first)') : jQuery(selector + ' option');
var arr = options.map(function(_, o) {
return {
t: jQuery(o).text(),
v: o.value,
s: jQuery(o).prop('selected')
};
}).get();
arr.sort(function(o1, o2) {
var t1 = o1.t.toLowerCase(),
t2 = o2.t.toLowerCase();
return t1 > t2 ? 1 : t1 < t2 ? -1 : 0;
});
options.each(function(i, o) {
o.value = arr[i].v;
jQuery(o).text(arr[i].t);
if (arr[i].s) {
jQuery(o).attr('selected', 'selected').prop('selected', true);
} else {
jQuery(o).removeAttr('selected');
jQuery(o).prop('selected', false);
}
});
}
jQuery(document).ready(function() {
sortSelectOptions('#cf7_id', true);
});
});
</script>
5. Go to the code block “Auxiliary” tab in the right and check “Website Backend”
6. Hit “Save”, and it’s done.
Obs:
This solution maintains the select first option in the same position
I changed the Stack Overflow code in some places from $ to jQuery in order to be compatible with my WP.
-
This reply was modified 6 years, 5 months ago by
andre2009. Reason: missing info
-
This reply was modified 6 years, 5 months ago by
andre2009.