Skip to content

Instantly share code, notes, and snippets.

@michaelbragg
Created September 14, 2015 12:13
Show Gist options
  • Save michaelbragg/1fc27a13af0fe28af7c2 to your computer and use it in GitHub Desktop.
Save michaelbragg/1fc27a13af0fe28af7c2 to your computer and use it in GitHub Desktop.
WordPress: Flush rewrite rules on plugin activation
<?php
/**
* `includes/class-*-activator.php
*/
/**
* Add setting to register the plugins activation.
*
* Create an option field in the database to check against later.
*
* @since 1.0.0
*/
public static function activate() {
add_option( 'Activated_Plugin', 'plugin-name' );
}
<?php
/**
* `admin/class-*-admin.php
*/
/**
* Flush rewrite rules when the option is present
*
* @since 1.0.0
*/
public function load_plugin() {
global $wp_rewrite;
if ( is_admin() && get_option( 'Activated_Plugin' ) == 'plugin-name' ) {
delete_option( 'Activated_Plugin' );
$wp_rewrite->init();
$wp_rewrite->flush_rules();
}
}
<?php
/**
* `includes/class-*.php
*/
private function define_admin_hooks() {
/** ... */
$this->loader->add_action( 'admin_init', $plugin_admin, 'load_plugin' );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment