发布到Wordpress admin_post,但需要代码在INIT上运行

I have a POST action I am using to create an XLS spreadsheet.

However, this spreadsheet won't create properly because if I put it within the POST action, when it runs, headers have already been sent.

So, I tried to put the XLS code into another function and then use another add_action to run it at INIT.

I put that add_action call within the POST action, but it doesn't work correctly.

I only want this function running at INIT if I am POSTing to it. I don't want it running no matter what/on every single INIT.

    function thexlsfunction() {
    //this is where the XLS code runs
    }



    function update_attendance_report_function() {
//this runs when I submit the form


    add_action('init', 'thexlsfunction');
    thexlsfunction();

    }

    add_action( 'admin_post_nopriv_attendance_report', 'update_attendance_report_function' );
    add_action( 'admin_post_attendance_report', 'update_attendance_report_function' );