Skip to content

Instantly share code, notes, and snippets.

@mt8
Created February 3, 2017 02:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mt8/ad7f9ab319570137b2ef64247ef7cb3a to your computer and use it in GitHub Desktop.
Save mt8/ad7f9ab319570137b2ef64247ef7cb3a to your computer and use it in GitHub Desktop.
特定のWordPressプラグインを管理画面メニューでON/OFFするプラグイン
<?php
/*
* Plugin Name: Plugin On Off
*/
$poo = new Plugin_On_Off();
$poo->register_hooks();
class Plugin_On_Off {
const TARGET_PLUGIN_NAME = 'WP BASIC Auth';
const TARGET_PLUGIN_FILE = 'wp-basic-auth/plugin.php';
public function register_hooks() {
add_action( 'admin_menu', array( $this, 'admin_menu' ) );
add_action( 'admin_print_footer_scripts', array( $this, 'admin_print_footer_scripts' ) );
}
public function admin_menu() {
$on_off = ( $this->is_target_active() ) ? ' Off' : ' On';
$label = self::TARGET_PLUGIN_NAME.$on_off;
add_menu_page( $label, $label, 'manage_options', 'plugin-on-off' );
}
public function admin_print_footer_scripts() {
$plugin_file = self::TARGET_PLUGIN_FILE;
if ( $this->is_target_active() ) {
$action_link = wp_nonce_url( 'plugins.php?action=deactivate&plugin=' . $plugin_file, 'deactivate-plugin_' . $plugin_file );
} else {
$action_link = wp_nonce_url( 'plugins.php?action=activate&plugin=' . $plugin_file, 'activate-plugin_' . $plugin_file );
}
$action_link = str_replace( '&amp;', '&', $action_link );
?><script>
jQuery(function($){
var menu_slug = 'plugin-on-off';
$('a.toplevel_page_' + menu_slug).prop({
href: "<?php echo $action_link; ?>",
target: "_blank"
});
});
</script><?php
}
public function is_target_active() {
return is_plugin_active( self::TARGET_PLUGIN_FILE ) ;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment