0

I'm building a Wordpress Plugin (structured as a Singleton) that, when activated, inserts three new WooCommerce Endpoints (each endpoint is stored in a separate file containing the Endpoint Class). Finally, a mandatory flush_rewrite_rules() is called. The flush must happen after the three endpoints have been inserted or the endpoints will 404 when visited.

I want to avoid having to activate 4 separate plugins (my plugin + 3 endpoints plugins). Just one plugin activation if possible would be preferred.

So far :

My plugin activates OK and the endpoints 'visually appear' OK in the navigation but will 404 when visited. and need a manual flush.

The key problems are:

  1. Where is best to include the three Endpoint class files inside my Singleton plugin? Top of Plugin? Inside the __construct? Or after the plugin?
  2. Where is best to call the Classes after including them?
  3. How to ensure flush_rewrite_rules is called after the three new endpoints have been inserted. Calling a flush_rewrite_rules() inside the register_activation_hook() doesn't seem to fix the 404ing endpoints.

Here is my abstracted code:

/* Include endpoint files here? */
include_once( plugin_dir_path( __FILE__ ) . 'classes/endpoint1.php');
include_once( plugin_dir_path( __FILE__ ) . 'classes/endpoint2.php');
include_once( plugin_dir_path( __FILE__ ) . 'classes/endpoint3.php');


class My_Plugin {

private $endpoint1 = null;
private $endpoint2 = null;
private $endpoint3 = null;

private static $instance;

public static function getInstance() {
  if (self::$instance == NULL) {
    self::$instance = new self();
  }

  return self::$instance;
}

private function __construct() {
  /*Call the endpoint classes here? */
  $this->$endpoint1 = new Endpoint1();
  $this->$endpoint1 = new Endpoint2();
  $this->$endpoint1 = new Endpoint3();
}
public static function install() {
  flush_rewrite_rules();
}
} /*End Class*/

My_Class::getInstance();

/*Flush rewrite rules here? */
register_activation_hook( __FILE__, array( 'My_Plugin', 'install' ) );

Any help appreciated <3

  • http://andrezrv.com/2014/08/12/efficiently-flush-rewrite-rules-plugin-activation/ – Christina Feb 08 '17 at 14:43
  • Thanks, thats an interesting approach using `add_option` as a flag to control the flushing. I just cant believe how much additional code is needed for such a simple job though haha. Thanks for sharing this. I'm going to hold out and see if any other possible solutions come forward – Allan Crabtree Feb 09 '17 at 12:13
  • I would post on http://wordpress.stackexchange.com/ -- you'll get more traffic to this question. – Christina Feb 09 '17 at 14:57

0 Answers0