Skip to content

Instantly share code, notes, and snippets.

@johnalarcon
Last active January 3, 2020 12:47
Embed
What would you like to do?
A few custom tweaks for the new ClassicPress Security page
/**
* Remove ClassicPress security page from admin menu and convert icon to text.
*
* Reasoning: After being initially in favor of the addition of a security page,
* the implementation seems rushed and incomplete. Since I already have a plugin
* for security, this means I have 2 entries for security in the top-level admin
* menu. This action hook removes the needless core menu item. This doesn't kill
* the security page's functionality; it only removes the admin menu item. For a
* plugin that has registered security settings to be displayed on that page, it
* will still have a "shield" icon on the plugin admin page leading to the right
* security page.
*
* Bonus: Convert shield icon back into text link in plugin admin rows.
*
* For a variety of reasons, I don't care for the shield icon being displayed in
* the plugin admin rows. It's not vertically centered, it has a divider next to
* it which looks weird, there's 1 icon and the rest are text links, and it will
* probably promote others to do the same with their own ideas about which icons
* are suitable for what. Also, text is 100% translatable; icons, not so much.
*/
/**
* Remove top-level admin menu item.
*/
function yourprefix_remove_security_menu() {
remove_menu_page('security.php');
}
add_action('admin_menu', 'yourprefix_remove_security_menu');
/**
* Hook in a function to create a text link instead of an icon.
*/
function yourprefix_replace_security_icon_with_text() {
if (!function_exists('\add_security_page')) {
return;
}
foreach (array_keys(get_plugins()) as $plugin) {
$func = '_security_page_action_links';
$hook = 'plugin_action_links_'.$plugin;
if (has_filter($hook, $func)) {
remove_filter($hook, $func, PHP_INT_MAX);
add_filter($hook, 'yourprefix_register_security_link_as_text', PHP_INT_MAX, 3);
}
}
}
add_action('load-plugins.php', 'yourprefix_replace_security_icon_with_text', PHP_INT_MAX);
/**
* The function that actually creates the Security text link.
*/
function yourprefix_register_security_link_as_text($links, $identifier, $details) {
$link = sprintf(
'<a href="%1$s?page=%2$s" title="%3$s">%3$s</a>',
admin_url('admin.php'),
str_replace('.php', '', substr($identifier, strpos($identifier, '/')+1)),
__('Security')
);
array_unshift($links, $link);
return $links;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment