我正在使用WordPress 4.9.6
我正在尝试创建一个插件,从外部API提取数据。
请参见以下我的当前代码:
myPlugin.php
<?php
/**
Plugin Name: myPlugin
description: Get Data via APIs
Version: 1.0
Author: Batman
License: GPLv2 or later
Text Domain: myPlugin
*/
if ( ! defined( \'ABSPATH\' ) ) {
exit;
}
require_once \'vendor/autoload.php\';
class My_Plugin {
/**
* Constructor.
*/
public function __construct() {
define( \'MyPlugin_FILE\', __FILE__ );
define( \'MyPlugin_DIR\', trailingslashit( dirname( __FILE__ ) ) );
define( \'MyPlugin_VERSION\', \'0.0.1\' );
register_activation_hook( basename( MyPlugin_DIR ) . \'/\' . basename( MyPlugin_FILE ), array( $this, \'activate\' ) );
add_action( \'plugins_loaded\', array( $this, \'includes\' ) );
add_action( \'init\', array( $this, \'maybe_update\' ) );
}
public function activate() {
$this->includes();
flush_rewrite_rules();
}
public function includes() {
include_once( MyPlugin_DIR . \'includes/WhatToMineAPI.php\' );
}
/**
* Maybe update MyPlugin.
*/
public function maybe_update() {
$version = get_option( \'MyPlugin_version\', 0 );
if ( version_compare( $version, MyPlugin_VERSION, \'<\' ) ) {
$this->create_tables();
update_option( \'MyPlugin_version\', MyPlugin_VERSION );
}
}
}
new My_Plugin();
WhatToMineAPI.php
<?php
use GuzzleHttp\\Client;
class WhatToMineAPI {
/**
* Constructor.
*/
public function __construct() {
add_action( \'setupCronJob_whatToMine\', \'setupCronJob\');
add_action( \'update_whatToMine_api\', \'updateWhatToMineAPI\');
// variables
$whatToMineURL = "http://whattomine.com/coins.json";
}
public function setupCronJob() {
//Use wp_next_scheduled to check if the event is already scheduled
$timestamp = wp_next_scheduled( \'update_whatToMine_api\' );
//If $timestamp == false schedule daily backups since it hasn\'t been done previously
if( $timestamp == false ){
//Schedule the event for right now, then to repeat daily using the hook \'update_whatToMine_api\'
wp_schedule_event( time(), \'twicedaily\', \'update_whatToMine_api\' );
}
}
public function updateWhatToMineAPI() {
$client = new GuzzleHttp\\Client();
$response = $client->request(\'GET\', $whatToMineURL);
}
}
new WhatToMineAPI();
我已经安装了插件WP-Control 了解我正在运行的cron作业的概况。然而,我的cron工作update_whatToMine_api
在表中找不到?我当前的cron设置是否错误?感谢您的回复!