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:
- Where is best to
include
the three Endpoint class files inside my Singleton plugin? Top of Plugin? Inside the__construct
? Or after the plugin? - Where is best to call the Classes after including them?
- How to ensure flush_rewrite_rules is called after the three new endpoints have been inserted. Calling a
flush_rewrite_rules()
inside theregister_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