As stated in the title, I had written a plugin that was pulling a blog post from a blog and then created a new blog post with that same data in the blog where the plugin is installed. When I put everything into a class, the plugin stops working. I believe it has something to do with the way I wrote the is in the __constructor function.
<?php
/**
*@package blog-poster
*/
/*
Plugin Name: Blog Poster
Plugin URI:
Description: This is a plugin for to be automatically posted.
Version: 1.0.0
Author: Ben Smith
Author URI:
*/
if ( ! defined( 'ABSPATH') ){
die;
}
class bgs_blog_poster {
function __constructor(){
//activation of plugin
register_activation_hook( __FILE__, array($this,'activate' ) );
//deactivation of plugin
register_deactivation_hook( __FILE__, array($this, 'deactivate' ) );
//adds 30 second interval to wp-cron
add_filter( 'cron_schedules', array($this, 'add_cron_recurrence_interval' ) );
//fires the action every 30 seconds
add_action('30_second_action', array($this, 'create_blog_post' ) );
}
function add_cron_recurrence_interval( $schedules ) {
$schedules['every_30_seconds'] = array(
'interval' => 10,
'display' => __( 'Every 30 Seconds', 'textdomain' )
);
return $schedules;
}
function activate() {
if ( ! wp_next_scheduled( '30_second_action' ) ) {
wp_schedule_event( time(), 'every_30_seconds', '30_second_action' );
}
}
function create_blog_post(){
require_once ABSPATH . '/wp-admin/includes/post.php'; //ask chad about this one
$jsondata = file_get_contents(''); //pulls json data from hansenlighting blog
$json = json_decode($jsondata,true); //turns json data into something that php can understand
$ranNum = rand(1,10); //generates random number between 1-10
$blogTitle = $json[$ranNum][title][rendered];
$blogContent = $json[$ranNum][content][rendered];
if ( post_exists($blogTitle) ) { // checks to see if that blog post exists. If it does it makes a new until until it can find one that doesn't
do {
$ranNum = rand(1,10); //remakes another random until and assigns
$blogTitle = $json[$ranNum][title][rendered];
$blogContent = $json[$ranNum][content][rendered];
}
while (post_exists($blogTitle));
$post = array(
'post_title' => $blogTitle,
'post_content' => $blogContent,
'post_status' => 'publish',
'post_author' => 1,
'post_type' => 'post',
);
wp_insert_post( $post ); //creates post
} else {
$post = array(
'post_title' => $blogTitle,
'post_content' => $blogContent,
'post_status' => 'publish',
'post_author' => 1,
'post_type' => 'post',
);
wp_insert_post( $post );
}
}
function deactivate(){
//wp_clear_scheduled_hook('my_hourly_event');
flush_rewrite_rules(); //may not need this since its not a custom post type
}
function uninstall(){
}
}
$blogPosterPlugin = new bgs_blog_poster();
?>