Moving the sort function out of another function and adding esc_url_raw() as per @Mo #24003