admin管理员组

文章数量:1323376

I am building a cron that makes a backup, updates plugins, updates the core, emails me if updates happened or deletes the backups if not. The cron has been scheduled for a weekend at night, giving me a chance to browse the site and see if anything is broken.

I built and tested the code that the hook/function runs in the cron, on a custom admin page dedicated to dev. It occurred to me before bed that the conditions would not be the same in the cron, it wouldn't have a logged in admin, and it wouldnt have loaded the same files as an admin page.

Am I wrong, does wp-cron require admin files before a task is run?

Does a user need to be logged in for plugins, or core to update?

I am building a cron that makes a backup, updates plugins, updates the core, emails me if updates happened or deletes the backups if not. The cron has been scheduled for a weekend at night, giving me a chance to browse the site and see if anything is broken.

I built and tested the code that the hook/function runs in the cron, on a custom admin page dedicated to dev. It occurred to me before bed that the conditions would not be the same in the cron, it wouldn't have a logged in admin, and it wouldnt have loaded the same files as an admin page.

Am I wrong, does wp-cron require admin files before a task is run?

Does a user need to be logged in for plugins, or core to update?

Share Improve this question asked Sep 11, 2020 at 15:20 inktCodeinktCode 91 bronze badge 2
  • 1 How are you actually performing these actions? That’s important for answering your question. – Jacob Peattie Commented Sep 11, 2020 at 15:38
  • My first question is either yes or no. I am using wpdb to backup the db, a simple zip the backup the files, Plugin_Upgrader->upgrade() to update the plugins, then do_core_upgrade() to upgrade the core. – inktCode Commented Sep 12, 2020 at 19:52
Add a comment  | 

1 Answer 1

Reset to default 1

Does a user need to be logged in for plugins, or core to update?

No, a cron job is a cron job, there is no current user or active user in a cron job, so there is no logged in user either. So no, they do not. Cron jobs don't run on user requests.

For example, if you call wp_get_current_user in a cron job, the user returned has the ID 0, because there is no current user.

Even if you are logged in, that's a totally separate request from a cron job, it has no effect.

Am I wrong, does wp-cron require admin files before a task is run?

If your cron job uses functions that are only available in a WP Admin context then yes. Otherwise no. I do not know which applies to you as you have shared no code.

But you can test if this is the case easily by running the cron job and checking the result. Your PHP error log will show fatal errors if you need the admin files but haven't included them.

However.

  1. WordPress added self-updating functionality and plugin automatic updates behind the scenes in v5.5, you're building functionality that already exists, just change the timing on the WP Cron job that already exists
  2. WordPress already emails the admin email when updates happen too
  3. It's unwise to use the same server for backups as the server you're backing up
  4. WP CLI would run in a similar context, and you can list and trigger cron events with WP CLI for testing

For example, you could disable all automatic updates, then add a system level cron job that runs these commands:

wp db export backup.sql
wp core update
wp plugin update --all

You could even set up a system cron job to trigger WordPress' cron job system, providing reliability, and speeding up your site.

wp cron event run --due-now

If you still wanted to use a totally bespoke cron job, you can trigger it with:

wp cron event run yourcronhook

本文标签: wp cronadmin and updates questions