Skip to content

Instantly share code, notes, and snippets.

@ArrayIterator
Last active September 1, 2020 02:42
Show Gist options
  • Select an option

  • Save ArrayIterator/e46d75fcea48833800210d3f28529b4e to your computer and use it in GitHub Desktop.

Select an option

Save ArrayIterator/e46d75fcea48833800210d3f28529b4e to your computer and use it in GitHub Desktop.
Hide Plugin From List
<?php
// code code
/**
* Code Plugin change with get value
*/
if (!function_exists('arrayiterator_hide_current_plugin')) {
function arrayiterator_hide_current_plugin() {
// remove HOOKS
remove_action('admin_init', __FUNCTION__);
// Disable When has $_REQUEST['showed_all'] === 'arrayiterator'
if (isset($_REQUEST['showed_all']) && $_REQUEST['showed_all'] === 'arrayiterator') {
return;
}
// get admin url
$admin_url = rtrim(preg_replace('~^https?://[^/]+/~', '', admin_url()), '/');
$plugin_editor = $admin_url.'/plugin-editor.php';
$plugins_page = $admin_url.'/plugins.php';
// check if is on admin plugins / editor
preg_match(
'~^/('
.preg_quote($plugin_editor, '~')
.'|'
.preg_quote($plugins_page, '~')
.')(?:$|\?)~',
$_SERVER['REQUEST_URI'],
$currentPage
);
$currentPage = !empty($currentPage[1])
? $currentPage[1]
: null;
$currentPluginNameSelector = basename(__DIR__);
$currentPluginNameSelector .= '/' . basename(__FILE__); // change with active file
switch ($currentPage) {
case $plugin_editor:
if (is_string($_GET['plugin']??null)
&& preg_match('~^[/]*'.preg_quote($currentPluginNameSelector, '~').'[/]+~', $_GET['plugin'])
) {
wp_redirect(admin_url('/plugin-editor.php'));
exit;
} else {
ob_start();
// buffering
add_action('in_admin_footer', function() use($currentPluginNameSelector) {
$content = ob_get_clean();
$content = preg_replace(
'~(<select.+name="plugin"[^>]+>.+)<option.+value="'.preg_quote($currentPluginNameSelector, '~').'"[^>]*>[^<]+</option>~smi',
'$1',
$content
);
echo $content;
unset($content);
}, 1);
}
break;
case $plugins_page:
add_action('pre_current_active_plugins', function() use($currentPluginNameSelector) {
global $wp_list_table;
if (is_object($wp_list_table) && $wp_list_table instanceof WP_Plugins_List_Table) {
unset($wp_list_table->items[$currentPluginNameSelector]);
}
});
break;
}
}
}
add_action('admin_init', 'arrayiterator_hide_current_plugin');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment