我正在使用一个简单的插件来计算帖子和帖子的FB份额 我想使用cron作业创建插件类的实例。 插件的主要部分是:
class PostShareCount
{
public function __construct()
{
global $wpdb;
$this->db = $wpdb;
add_action('hourly_event', 'updateAllPostsShares');
add_shortcode('social-count', array($this, 'social_shares'));
}
public function updateAllPostsShares()
{
$this->db->query('CREATE TABLE IF NOT EXISTS wp_facebook_shares(
post_id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
post_shares INT NOT NULL,
post_url VARCHAR(255) NOT NULL
)');
$posts = $this->db->get_results('SELECT ID, post_date, post_name FROM '.$this->db->posts.' WHERE ID < 3');
$fb_graph = 'http://graph.facebook.com/?id=';
$site = 'http://www.example.com/';
$posts_shares = [];
foreach ($posts as $post) {
$post_url = $site.$post->post_name;
$posts_shares[$post->ID] = array();
$posts_shares[$post->ID]['post_id'] = $post->ID;
$posts_shares[$post->ID]['post_url'] = $post_url;
$api_call = $fb_graph.$site.$post->post_name;
if (isset($this->get_response_body($api_call)->shares)) {
$posts_shares[$post->ID]['post_shares'] = $this->get_response_body($api_call)->shares;
} else {
$posts_shares[$post->ID]['post_shares'] = rand(80, 1200);
}
$this->db->replace('wp_facebook_shares', $posts_shares[$post->ID], array('%d', '%s', '%d'));
}
return $posts_shares;
}
}
为了测试我的cron,我创建了这个简单的文件:
<?php
require_once ('post-share-count.php');
$obj = new PostShareCount();
$obj->updateAllPostsShares();
?>
但每当我尝试运行它时,我都会收到此错误:
Call to undefined function add_action() in ..
知道造成这种情况的原因以及如何解决这个问题? THX
答案 0 :(得分:3)
安装WP Crontrol插件。 现在,你可以从管理中运行你的crons。
如果由于某种原因需要直接从测试PHP文件中调用插件函数,请不要忘记包含wp-load.php。
示例(文件与wp-load.php位于同一目录中):
<?php
$con = mysqli_connect("127.0.0.1","superinfoadmin","","superhero");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
if($_SERVER['REQUEST_METHOD']=='POST'){
$minvalue = $_POST['minvalue'];
$maxvalue = $_POST['maxvalue'];
$sql="SELECT * FROM superinfo, publisher WHERE
superinfo.s_publisherid=publisher.p_id AND s_power BETWEEN '$minvalue' AND
'$maxvalue'";
echo $sql;
$result = mysqli_query($con,$sql);
$output=array();
$output = mysqli_fetch_assoc($result);
print(json_encode($output));
mysqli_close($con);
}
?>