Created
September 14, 2015 12:13
-
-
Save michaelbragg/1fc27a13af0fe28af7c2 to your computer and use it in GitHub Desktop.
WordPress: Flush rewrite rules on plugin activation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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' ); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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