How make a php script run only with wp-cron

WP_Cron isn’t a real cron scheduler.

What it does is schedule events, mark when they should be run, and wait for page traffic. When the site is loaded, it checks to see if any of these tasks need to be run (i.e. the time they were scheduled for has passed) and runs them. So you see, WordPress’ cron system is entirely dependent on your site receiving traffic.

However, it doesn’t run the scheduled code in the same process. WordPress posts back to itself (i.e. loading another copy of WordPress for a separate request) and runs the code there. If you’re seeing a delay in your page load due to this script running, then it means you have something else going on.

Also, your call to wp_schedule_event is wrong. The first parameter should be the time you want the task to run … not “now” if you only want things to run once per day. Rewrite this to:

wp_schedule_event( time() + 24 * 60 * 60, 'daily', 'my_cron_hook' );

Finally, if this PHP script can run outside of WordPress, you’d be much better suited to schedule a regular event in your system’s crontab. Your system administrator or host can help with that directly.