template_redirect to accompany with a shortcode

The answer below doesn’t directly answer your question but provides a possible alternative solution.

Drop has_shortcode( $post->post_content, 'my-shortcode' ) statement. There is no need to check this if you are validating nonce. So the validation should look something like this.

function project_registration_login_redirection(){

    if ( !isset( $_POST['my_submit'] ) ) {
        return;
    }

    // form validation here
    global $errors; // a global var so you can show errors on your form if any

    // I usually have a seperate validation class but you may do it different way.
    // this method is useful to feed mock array for testing purpose
    $validator = new My_Form_validator();
    $errors = $validator->validate($_POST);

    if(!empty($errors){
        return;
    }

    // no error found

    $data = $validator->get_data();
    // process form data here

    wp_redirect(home_url('that-page'));
    exit;

}
add_action( 'template_redirect', 'project_registration_login_redirection' );

hope it helps.

Leave a Comment