How can I run a WP function on server cron?
Background: I can't run it on wp_cron because the website is not published and will not be, so there will be no visitors. Tested cron job without wp function, it is working properly (like sending email every 30 minutes).
//RemoteCoins() and LoadRemoteCoins() that include wp functions like wp_remote_get()
function runthe_func() {
//some code here
}
runthe_func();
I kept on getting errors like: "Fatal error: Call to undefined function wp_remote_get() in /home/path/path2/testing.php on line 91"
How can I make it run with the server recognizing wp functions?
How can I run a WP function on server cron?
Background: I can't run it on wp_cron because the website is not published and will not be, so there will be no visitors. Tested cron job without wp function, it is working properly (like sending email every 30 minutes).
//RemoteCoins() and LoadRemoteCoins() that include wp functions like wp_remote_get()
function runthe_func() {
//some code here
}
runthe_func();
I kept on getting errors like: "Fatal error: Call to undefined function wp_remote_get() in /home/path/path2/testing.php on line 91"
How can I make it run with the server recognizing wp functions?
Share Improve this question asked May 17, 2019 at 2:25 kriskrosskriskross 131 silver badge3 bronze badges 3 |2 Answers
Reset to default 1We need to include wp-load.php
in order to bootstrap WordPress in our custom PHP code. So, the modified code would look something like this...
include '/path/to/wp-load.php';
//RemoteCoins() and LoadRemoteCoins() that include wp functions like wp_remote_get()
function runthe_func() {
//some code here
}
runthe_func();
Now, if we run this file on server cron, it wouldn't throw errors.
Oh, btw, similar questions have been asked and answered multiple times here in WPSE. For example, How to load WordPress on non WP page? .
@kriskross Welcome to the Community.
First of all, you need to create a scheduled event for WordPress cron jobs. You need to stay in functions.php
(placed on your themes) or related files (plugin files etc.) for calling WordPress core functions. Also, you can create cron jobs from server side.
if you want to schedule please read and follow these instructions.
Create Server Handled Cron Jobs
place this configuration in your wp-config.php file before That's all, stop editing! Happy blogging.
define('DISABLE_WP_CRON', true);
after that, You need to create cron jobs from you local server (You said my website will not be released or get any visitors).
Here is the web based cron trigger link.
wget -q -O - http://yourdomain/wp-cron.php?doing_wp_cron >/dev/null 2>&1
You can use online crontab generator like websites for timing and crontab.
I hope this will be your solution.
wp-load.php
in order to let your custom function recognize WP functions. – Pothi Kalimuthu Commented May 17, 2019 at 3:54