0

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: http://www.example.com
Description: This is a plugin for to be automatically posted.
Version: 1.0.0
Author: Ben Smith
Author URI: http://www.example.com
*/
  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('http://blog.hansenlighting.com/wp-json/wp/v2/posts?per_page=20'); //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();


?>

  • 2
    It's function __construct and not function __constructor .. – Sally CJ Oct 8 '19 at 17:05
  • thanks for pointing that out, I changed it to function __construct and it still isn't working. when I remove it from the function __construct and change $this to BGS_Blog_Poster the code works but it gives me a few error warnings. – bsmith Oct 8 '19 at 18:02
  • 1
    I got it to work! I forgot to initialize the class. What you mentioned before was the real issue. Thank you for your help. – bsmith Oct 8 '19 at 18:06
  • Glad it helped. :) And you shouldn't change $this to BGS_Blog_Poster because your functions are not static - not defined using the static keyword as in static function activate(). – Sally CJ Oct 8 '19 at 18:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.