How can I put a wp_redirect into a shortcode?

Shortcode functions are only called when the content of the visual editor is processed and displayed, so nothing in your shortcode function will run early enough.

Have a look at the has_shortcode function. If you hook in early enough to send headers and late enough for the query to be set up you can check if the content contains your shortcode and redirect then. The template_redirect hook is handy for this as it’s about the last hook to be called before your theme sends output to the browser, which triggers PHP to send the headers.

Leave a Comment