Admin

I’ve gotten in the habit of always including a file to handle some core customizations. Here are some notes about the functions included and what they do. All of these are included in the /inc/wp-cleaner.php file.

Howdy

I dig ‘Howdy’. Some folks may not, so I remove it. In some situations it may be a bit too casual for topic of the publication.

add_filter( 'admin_bar_menu', 'dwp23_replace_wordpress_howdy', 25 );
function dwp23_replace_wordpress_howdy( $wp_admin_bar ) {
  $account = $wp_admin_bar->get_node('my-account');
  $newtext = str_replace( 'Howdy,', '', $account->title );
  $wp_admin_bar->add_node( array(
    'id' => 'my-account',
    'title' => $newtext,
  ));
}

add_filter( 'gettext', 'dwp23_change_howdy_text', 10, 2 );
function dwp23_change_howdy_text( $translation, $original ) {
  if( 'Howdy, %1$s' == $original )
    $translation = '%1$s';
  return $translation;
}

Log In / Out

There are a couple of things I like to customize here. I like to have an easy way for users to log in and out, so I add a small icon to the end of the bottom of the site which links to the login form if you’re not logged in and logs out otherwise.

add_filter( 'wp_nav_menu_items', 'dwp23_loginout_menu_link', 10, 2 );
function dwp23_loginout_menu_link( $items, $args ) {
  if ($args->theme_location == 'bottom') {
    if (is_user_logged_in()) {
      $items .= '<li class="nav-item"><a class="navbar-brand" href="'. wp_logout_url() .'"><i class="bi bi-person-circle fs-5"></i></a></li>';
    } else {
      $items .= '<li class="nav-item"><a class="navbar-brand" href="'. wp_login_url() .'"><i class="bi bi-person-circle fs-5"></i></a></li>';
    }
  }
  return $items;
}

I like to customize the login form with the same theme colors and site logo so it’s unified presentation.

add_action( 'login_enqueue_scripts', 'dwp23_login_logo' );
function dwp23_login_logo() { ?>
    <style type="text/css">
      #login h1 a, .login h1 a {
        background-image: url(<?php echo get_stylesheet_directory_uri(); ?>/img/android-chrome-192x192.png) !important;
        background-repeat: no-repeat;
        padding-bottom: 10px;
      }
    </style>
<?php }
add_filter( 'login_headerurl', 'dwp23_login_url' );
function dwp23_login_url() {  return home_url(); }
add_filter( 'login_headertext', 'dwp23_login_title' );
function dwp23_login_title() { return get_option( 'blogname' ); }

And lastly, I like to be able to see who was in or out, so I add a function to display the users last login time in the users table.

add_action( 'wp_login', 'dwp23_login_timestamp', 20, 2 );
function dwp23_login_timestamp( $user_login, $user ) {
  update_user_meta( $user->ID, 'last_login', time() );
}
add_filter( 'manage_users_columns', 'dwp23_user_last_login_column' );
function dwp23_user_last_login_column( $columns ) {
  $columns['last_login'] = 'Last Login'; // column ID / column Title
  return $columns;
}
add_filter( 'manage_users_custom_column', 'dwp23_last_login_column', 10, 3 );
function dwp23_last_login_column( $output, $column_id, $user_id ){
  if( $column_id == 'last_login' ) {
    $last_login = get_user_meta( $user_id, 'last_login', true );
    $date_format = 'j M, Y';
    $output = $last_login ? date( $date_format, $last_login ) : '-';
  }
  return $output;
}
add_filter( 'manage_users_sortable_columns', 'dwp23_sortable_columns' );
function dwp23_sortable_columns( $columns ) {
  return wp_parse_args( array(
   'last_login' => 'last_login'
  ), $columns );
}
add_action( 'pre_get_users', 'dwp23_sort_last_login_column' );
function dwp23_sort_last_login_column( $query ) {
  if( !is_admin() ) { return; }
  $orderby = $query->get('orderby');
  if( 'last_login' == $orderby ) {
    $query->set('meta_key','last_login');
    $query->set('orderby','meta_value');
  }
  return $query;
}

Admin Bar

The admin bar is distracting. I turn it off on the front end, remove the options to enable it, and remove all of the menu items from it except the user options, home link, and my dark and development mode toggles. I remove the WordPress link and replace the home icon with the WordPress icon.

add_filter( 'show_admin_bar', '__return_false' );

add_action( 'admin_bar_menu', 'dwp23_remove_adminbar', 999 );
function dwp23_remove_adminbar( $wp_admin_bar ) {
  $wp_admin_bar->remove_node( 'wp-logo' );
  $wp_admin_bar->remove_node( 'updates' );
  $wp_admin_bar->remove_menu('comments');
  $wp_admin_bar->remove_menu('customize');
  $wp_admin_bar->remove_menu('new-content');
  $wp_admin_bar->remove_menu( 'edit' );
  $wp_admin_bar->remove_menu( 'new-user' );
  $wp_admin_bar->remove_menu( 'new-post' );
  $wp_admin_bar->remove_menu( 'new-page' );
  $wp_admin_bar->remove_node( 'new-media' );
  $wp_admin_bar->remove_menu( 'comments' );
  $wp_admin_bar->remove_node( 'view-site' );
}
tr.show-admin-bar {
  display:none;
}

I’ve always thought that pages are more important than posts in the taxonomy structure of a site. I bring the pages to the top, put the media library between two separators, and hide the other admin items from everyone but a couple users. I think this helps simplify the admin and makes it easier to use.

add_filter( 'custom_menu_order', 'dwp23_menu_order' );
add_filter( 'menu_order', 'dwp23_menu_order' );
function dwp23_menu_order( $menu_order ) {
  if (!$menu_order) return true;
  return array(
    'index.php',
    'separator1',
    'edit.php?post_type=page',
    'edit.php',
    'edit-comments.php',
    'separator2',
    'upload.php',
    'separator-last',
    'users.php',
    'theme-settings',
    'themes.php',
    'plugins.php',
    'tools.php'
  );
}

add_action( 'admin_menu', 'dwp23_remove_admin_pages', 99 );
function dwp23_remove_admin_pages() {
  global $current_user;
  $user_id = get_current_user_id();
  if($user_id != '23') {
    remove_submenu_page('themes.php', 'themes-editor.php');
    remove_submenu_page( 'themes.php', 'customize.php?return=' . urlencode($_SERVER['SCRIPT_NAME']));
    remove_submenu_page( 'themes.php', 'themes.php' );
    remove_submenu_page( 'themes.php', 'theme-editor.php' );
    remove_submenu_page( 'themes.php', 'theme_options' );
    remove_menu_page('plugins.php');
    remove_menu_page('tools.php');
    remove_menu_page('options-general.php');
    remove_menu_page('gutenberg');
  }
}

I replace the admin title with the name of the site. I remove some of the header links. Although I don’t think the Microsoft Windows Live Writer needs to be in there because it’s been discontinued, I leave it in my functions for nostalgia. And as much as I’m a fan of RSS, in many cases those are unneeded. The canonical and rel_links are up for debate, but I remove them more often than not. Same goes for the emojis for many sites. In development I replace the CSS and JS version scripts with a random number so that I’m always loading an uncached version and I remove those for production.

add_filter('admin_title', 'dwp23_admin_title', 10, 2);
function dwp23_admin_title($admin_title, $title) {
  return get_bloginfo('name').' &bull; '.$title;
}

add_action( 'init', 'dwp23_cleaner_header' );
function dwp23_cleaner_header() {
  remove_action('wp_head', 'index_rel_link' );
  remove_action('wp_head', 'rel_canonical');
  remove_action('wp_head', 'start_post_rel_link', 10);
  remove_action('wp_head', 'adjacent_posts_rel_link_wp_head', 10);
  remove_action('wp_head', 'wp_shortlink_wp_head', 10);
  remove_action('wp_head', 'parent_post_rel_link', 10);
  remove_action('wp_head', 'wlwmanifest_link');
  remove_action('wp_head', 'wp_generator');
  remove_action('wp_head', 'feed_links_extra', 3 );
  remove_action('wp_head', 'feed_links', 2 );
}

add_action( 'init', 'dwp23_disable_emojis' );
function dwp23_disable_emojis() {
  remove_action( 'wp_head', 'print_emoji_detection_script', 7 );
  remove_action( 'admin_print_scripts', 'print_emoji_detection_script' );
  remove_action( 'wp_print_styles', 'print_emoji_styles' );
  remove_action( 'admin_print_styles', 'print_emoji_styles' );
  remove_filter( 'the_content_feed', 'wp_staticize_emoji' );
  remove_filter( 'comment_text_rss', 'wp_staticize_emoji' );
  remove_filter( 'wp_mail', 'wp_staticize_emoji_for_email' );
  add_filter( 'emoji_svg_url', '__return_false' );
}

//add_filter( 'style_loader_src', 'dwp23_remove_wp_ver_css_js', 9999 );
//add_filter( 'script_loader_src', 'dwp23_remove_wp_ver_css_js', 9999 );
function dwp23_remove_wp_ver_css_js( $src ) {
  if ( strpos( $src, 'ver=' ) )
    $src = remove_query_arg( 'ver', $src );
  return $src;
}

I only put the WordPress version in the footer of the admin.

add_filter ('update_footer', 'dwp23_footer_ver', 999);
function dwp23_footer_ver ($default) {
  return ''. get_bloginfo( 'version' );
}

add_filter ('admin_footer_text', 'dwp23_footer_filter');
function dwp23_footer_filter ($default) {
  return '';
}

Welcome Panel

I always remove the welcome panel on sites. I’m all about giving credit and supporting the larger open source community, but it’s a more seamless experience for the individual user not to see the panel.

remove_action( 'welcome_panel', 'wp_welcome_panel' );