Skip to content

Instantly share code, notes, and snippets.

@strsar
Created October 7, 2021 01:40
Show Gist options
  • Save strsar/6f0573b1084b051a7c06922d4e1554c7 to your computer and use it in GitHub Desktop.
Save strsar/6f0573b1084b051a7c06922d4e1554c7 to your computer and use it in GitHub Desktop.
[WP] Admin starting point after some cleanup
<?php defined('ABSPATH') or header('Location: /');
/**
* Cleanup WP admin and unwanted code...
*/
// Remove actions
remove_action('welcome_panel', 'wp_welcome_panel');
remove_action('dashboard_primary', 'wp_dashboard_primary');
remove_action('admin_color_scheme_picker', 'admin_color_scheme_picker');
// Add actions
add_action('init', 'remove_comment_support');
add_action('admin_menu', 'clean_admin_menu');
add_action('admin_bar_menu', 'clean_admin_menubar', 999);
add_action('wp_dashboard_setup', 'remove_dashboard_widgets', 9999);
add_action('admin_init', 'disable_yoast_notifications', 999);
//add_action('admin_enqueue_scripts', 'admin_style');
// Add filters
add_filter('admin_head', 'clean_admin_submenu', 999);
add_filter('admin_head', 'remove_contextual_tabs', 999);
add_filter('admin_head', 'custom_admin_styles', 999);
add_filter('gettext', 'greeting', 10, 3);
add_filter('admin_footer_text', '__return_false', 9999);
add_filter('update_footer', 'text_version', 9999);
add_filter('acf/settings/save_json', 'acf_json');
add_filter('acf/settings/load_json', 'acf_json_load');
add_filter('user_contactmethods', 'remove_user_contact_methods');
add_filter('option_show_avatars', '__return_false');
add_filter('editable_roles', 'acl_filter_editable_roles');
// Required plugins
if(!function_exists('acf_add_options_page')){
add_action('admin_notices', 'plugin_dependency_notice');
}
/**
* Theme dependencies
*/
function plugin_dependency_notice(){
?>
<div class="notice notice-error is-dismissible">
<p>You must install <a href="https://www.advancedcustomfields.com/pro/" target="_blank">Advanced Custom Fields PRO</a> for this theme to function properly.</p>
</div>
<?php
}
/**
* Yoast SEO Disable Redirect Notifications for
* Posts or Pages: Moved to Trash
*/
add_filter('wpseo_enable_notification_post_trash', '__return_false');
/**
* Yoast SEO Disable Redirect Notifications for
* Posts and Pages: Change URL
*/
add_filter('wpseo_enable_notification_post_slug_change', '__return_false');
/**
* Yoast SEO Disable Redirect Notifications for
* Taxonomies: Moved to Trash
*/
add_filter('wpseo_enable_notification_term_delete', '__return_false');
/**
* Yoast SEO Disable Redirect Notifications for
* Taxonomies: Change URL
*/
add_filter('wpseo_enable_notification_term_slug_change', '__return_false');
/**
* Lower Yoast SEO metabox priority
*/
add_filter('wpseo_metabox_prio', function($html) {
return 'low';
});
/**
* Disable yoast notifications
*/
function disable_yoast_notifications() {
if(class_exists('Yoast_Notification_Center')) {
$notification_center = Yoast_Notification_Center::get();
$notification_center->deactivate_hook();
}
}
/**
* Save acf json
*/
function acf_json($path) {
$path = get_stylesheet_directory().'/acf-json';
return $path;
}
/**
* Load ACF json
* @param $paths
*
* @return array
*/
function acf_json_load( $paths ) {
unset($paths[0]);
$paths[] = get_stylesheet_directory() . '/acf-json';
return $paths;
}
/**
* Change default greeting
*/
function greeting($translated_text, $text, $domain) {
$new_message = str_replace('Howdy', 'Welcome', $text);
return $new_message;
}
/**
* Remove support
*/
function remove_comment_support() {
$post_types = get_post_types();
foreach($post_types as $type){
remove_post_type_support($type, 'comments');
remove_post_type_support($type, 'trackbacks');
}
}
/**
* Admin stylesheet
*/
function admin_style() {
wp_enqueue_style('admin-styles', get_template_directory_uri().'/css/admin.css');
}
/**
* Clean admin menu
*/
function clean_admin_menu() {
remove_menu_page('edit.php');
remove_menu_page('edit-comments.php');
}
/**
* Clean admin submenu
*/
function clean_admin_submenu() {
?>
<style>
#adminmenu .toplevel_page_wpseo_dashboard ul li:last-child,
#adminmenu .menu-icon-appearance ul .hide-if-no-customize {display:none}
</style>
<?php
}
/**
* Clean admin menubar
*/
function clean_admin_menubar($wp_admin_bar) {
$wp_admin_bar->remove_node('wp-logo');
$wp_admin_bar->remove_menu('new-post');
$wp_admin_bar->remove_menu('comments');
$wp_admin_bar->remove_menu('customize');
}
/**
* Admin styles in head
*/
function custom_admin_styles() {
echo "<style>
#yoast-helpscout-beacon, .yoast_premium_upsell,
.wp-admin.toplevel_page_wpseo_dashboard .yoast-sidebar,
.wp-admin.seo_page_wpseo_titles .yoast-sidebar,
.wp-admin.seo_page_wpseo_search_console .yoast-sidebar,
.wp-admin.seo_page_wpseo_social .yoast-sidebar,
.wp-admin.seo_page_wpseo_tools .yoast-sidebar { display:none }
</style>";
}
/**
* Remove user/author meta fields
*/
function remove_user_contact_methods($methods){
$methods = array(); // Return blank array to remove all contact methods
return $methods;
}
/**
* Users cannot create admins unless they are one
*/
function acl_filter_editable_roles( $all_roles ) {
if(!current_user_can('administrator')) {
unset($all_roles['administrator']);
}
return $all_roles;
}
/**
* Remove dashboard widgets
*/
function remove_dashboard_widgets() {
remove_meta_box('dashboard_primary', 'dashboard', 'side');
remove_meta_box('dashboard_quick_press', 'dashboard', 'side');
remove_meta_box('wpseo-dashboard-overview', 'dashboard', 'normal');
}
/**
* Remove admin help tabs
*/
function remove_contextual_tabs(){
$screen = get_current_screen();
$screen->remove_help_tabs();
}
/**
* Version display
*/
function text_version() {
global $wp_version;
echo '<small>v'. $wp_version.'</small>';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment