简体   繁体   中英

Wordpress PHP Cron jobs

I have a plugin that imports csv data into wordpress tables. It runs through the Wordpress dashboard where you input some details and click a button to execute it.

I've altered the plugin so that the input data is static(from the same csv every time) and is now all located in one php file. I want to schedule a Cron job to execute this script every hour or so.

I tried to set it up using cPanel and directly accessing the php file but it does not work(nothing is displayed). I believe this is because the plugin uses wordpress functions such as wp_insert_post.

How can i run this script, as if it were run through wordpress dashboard, as a scheduled event?

Note: the file also contains some javascript.

You've got to include 2 files to get access to admin side functions: First, wp-load.php. wp-load.php gets everything set up, and fires up wordpress. However, you are calling this function from the plugin folder, inside the content directory (as opposed to the admin directory) – so when wp-load is called, you are not going to be in the admin section, and not going to get access to those functions. On the bright side, you also don't have to deal with WordPress forcing you to login. Since you still need those admin functions, include wp-admin/admin-functions.php. This loads up the admin side and gives you access to the admin functions – and you are set to go

You should use wp_schedule_event. See in WordPress codex here : http://codex.wordpress.org/Function_Reference/wp_schedule_event . Use something like this:

            register_activation_hook(__FILE__, 'my_activation');
            add_action('my_hourly_event', 'do_this_hourly');

            function my_activation() {
                wp_schedule_event( time(), 'hourly', 'my_hourly_event');
            }

            function do_this_hourly() {
                // do something every hour
            }

You can perfectly call a PHP file inside your do_this_hourly() function.

Accepted answer worked well for me. I modded this, and here are my findings. It is the case that you can use this solution outside of a plugin scenario. Basically, you can have your server run using cron jobs using native wordpress functionality as followed. You can create a file at the top level of your wordpress app and then include /wp-load.php; therein.

This loads in native wordpress and allows you to call a class that you can define as part of your must use plugins. Then your good to go regarding basic wordpress functionality, things like $wpdb and get_usermeta()

Now you can instantiate your class with $class_variable = new YourClass; and from there you can call class functions. The great part about this is that you can schedule cron jobs as you would normally on your server, and when you do you have your wordpress function running as it would inside wordpress. This means you don't need to maintain your cron job stuff as part of a plugin, which may or may not be useful depending on what your up to.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM