How to put a form with php code into a variable or shortcode?

I would suggest that you remove the php from the form and instead write out the relevant HTML in your shortcode code.

  • Loop through wp_dropdown_categories and create a dropdown

  • Loop through clocations and build your collection of checkboxes

  • Drop the wp_nonce_field and code up something simple yourself using a hidden field.

Essentially the code doesn’t change much, it just outputs to a string instead. Once your shortcode puts out pure HTML, it can be wrapped inside another.