skip to Main Content

I am working on a wordpress website.
I want to do some updates on the products via a cpanel cron job.
for that I created a new file at the root /public_html/update-products.php
and on cpanel added the cron job command:

/usr/local/bin/php /home1/blahblah/public_html/update-products.php

my problem is that I want to use wordpress functions in this file like get_option, update_option, update_post_meta … etc

what file from wordpress should I include at the top of my file. I tried to include the index.php file itself so I can use such functions.

here is the file code (update-products.php)

require_once __DIR__ . '/index.php';

require_once __DIR__ . '/wp-content/plugins/citycenter-products/includes/api.php';

$last_update_info = get_option(DS_CITYCENTER_PRODUCTS_LAST_UPDATE_COMPACT_OPTION);

$data = array('type' => 'compact');
if ($last_update_info) {
    $data['from_datetime'] = $last_update_info['date'].' '.$last_update_info['time'];
}

$result = update_citycenter_products($data);

$last_update_info = array(
    'type' => 'compact',
    'date' => date('Y-m-d'),
    'time' => date('H:i:s'),
    'total' => $result['total'],
    'updated' => $result['updated'],
    'added' => $result['added'],
);
update_option(DS_CITYCENTER_PRODUCTS_LAST_UPDATE_COMPACT_OPTION, $last_update_info);

2

Answers


  1. This is because your file is outside of WordPress, so in the end a standalone PHP script.

    Take a look at WordPress documentation about the CRON job. WordPress has its own way of handling CronJob properly. Following this method, you’ll have access to all the WordPress ecosystem (methods and stuff).

    If you really want to load the WordPress ecosystem in your standalone script, it’s the wp-load.php file that is needed. Check this stack.

    Login or Signup to reply.
  2. Replace with require_once __DIR__ . '/wp-load.php'; instead of
    require_once __DIR__ . '/index.php';

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search