[wp-trac] [WordPress Trac] #57475: wp_schedule_event function causes "Headers already sent" error

WordPress Trac noreply at wordpress.org
Tue Jan 17 02:38:47 UTC 2023


#57475: wp_schedule_event function causes "Headers already sent" error
------------------------------+------------------------------
 Reporter:  onetarek          |       Owner:  (none)
     Type:  defect (bug)      |      Status:  closed
 Priority:  normal            |   Milestone:  Awaiting Review
Component:  Cron API          |     Version:  6.1.1
 Severity:  normal            |  Resolution:  invalid
 Keywords:  has-testing-info  |     Focuses:
------------------------------+------------------------------
Changes (by onetarek):

 * status:  new => closed
 * resolution:   => invalid


Comment:

 Replying to [comment:2 peterwilsoncc]:
 > The warnings in your logs are occurring when the cron job
 `test_daily_event_hook` runs, they're not running due to the call to
 `wp_schedule_event()`.
 >
 > Calls to wp-cron.php send the headers early in the request. The
 unconditional call to start the session in your plugin will then attempt
 to start a session on the wp-cron.php request.
 >
 >
 > {{{
 > [16-Jan-2023 21:36:55 UTC] PHP Stack trace:
 > [16-Jan-2023 21:36:55 UTC] PHP   1. {main}() /vagrant/wordpress-
 develop/src/wp-cron.php:0
 > [16-Jan-2023 21:36:55 UTC] PHP   2. require_once() /vagrant/wordpress-
 develop/src/wp-cron.php:46
 > [16-Jan-2023 21:36:55 UTC] PHP   3. require_once() /vagrant/wordpress-
 develop/src/wp-load.php:50
 > [16-Jan-2023 21:36:55 UTC] PHP   4. include() /vagrant/wordpress-
 develop/src/wp-config.php:9
 > [16-Jan-2023 21:36:55 UTC] PHP   5. require_once() /vagrant/wp-
 config.php:147
 > [16-Jan-2023 21:36:55 UTC] PHP   6. include_once() /vagrant/wordpress-
 develop/src/wp-settings.php:447
 > [16-Jan-2023 21:36:55 UTC] PHP   7. session_start()
 /vagrant/content/plugins/cron-test-plugin/plugin.php:24
 > }}}
 >
 > WP-cron doesn't support headers or sessions as it can be run either
 within the browser or via the CLI. I suggest you wrap the call to
 `session_start()` in a conditional to ensure it only runs in browser
 requests.
 >

 @peterwilsoncc **You are right**. The header is being sent from wp-
 cron.php. I put my code in a condition.
 {{{#!php
 <?php
 if( "" == session_id() && !defined( 'DOING_CRON' ) && !isset(
 $_GET['doing_wp_cron'] ) ){
     session_start();
 }
 }}}

 **Now there is no warning.**

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/57475#comment:3>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list