Skip to content

Instantly share code, notes, and snippets.

@nathaningram
Last active April 14, 2024 08:55
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nathaningram/daaff064b2e6d88854d4e8b688b8114b to your computer and use it in GitHub Desktop.
Save nathaningram/daaff064b2e6d88854d4e8b688b8114b to your computer and use it in GitHub Desktop.
Creating a Starter Site - Custom Functions - Core
<?php
/*
Plugin Name: Custom Core Functions
Plugin URI: https://nathaningram.com
Description: Customize the WP Core
Version: 2023.11
Author: Nathan Ingram
Author URI: https://nathaningram.com
License: GPL2
*/
// Security Check
if (!defined('ABSPATH')) {
die();
}
//Disable Automatic Theme and Plugin Autoupdates
add_filter( 'plugins_auto_update_enabled', '__return_false' );
add_filter( 'themes_auto_update_enabled', '__return_false' );
// Disable Autoupdate Emails
add_filter( 'auto_plugin_update_send_email', '__return_false' );
add_filter( 'auto_theme_update_send_email', '__return_false' );
// Disable Emojis
function ni_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( 'tiny_mce_plugins', 'ni_disable_emojis_tinymce' );
add_filter( 'wp_resource_hints', 'ni_disable_emojis_remove_dns_prefetch', 10, 2 );
}
add_action( 'init', 'ni_disable_emojis' );
function ni_disable_emojis_tinymce( $plugins ) {
if ( is_array( $plugins ) ) {
return array_diff( $plugins, array( 'wpemoji' ) );
} else {
return array();
}
}
function ni_disable_emojis_remove_dns_prefetch( $urls, $relation_type ) {
if ( 'dns-prefetch' == $relation_type ) {
$emoji_svg_url = apply_filters( 'emoji_svg_url', 'https://s.w.org/images/core/emoji/2/svg/' );
$urls = array_diff( $urls, array( $emoji_svg_url ) );
}
return $urls;
}
// Disable Feeds
function ni_clean_feeds() {
// Redirects all feeds to home page.
$url = site_url();
wp_redirect( $url );
}
add_action( 'do_feed', 'ni_clean_feeds', 1 );
add_action( 'do_feed_rdf', 'ni_clean_feeds', 1 );
add_action( 'do_feed_rss', 'ni_clean_feeds', 1 );
add_action( 'do_feed_rss2', 'ni_clean_feeds', 1 );
add_action( 'do_feed_atom', 'ni_clean_feeds', 1 );
add_action( 'do_feed_rss2_comments', 'ni_clean_feeds', 1 );
add_action( 'do_feed_atom_comments', 'ni_clean_feeds', 1 );
// Clean Up WP_Head
remove_action( 'wp_head', 'wp_generator' ); // Removes WordPress version.
remove_action( 'wp_head', 'wp_shortlink_wp_head', 10, 0 ); // Removes shortlink.
remove_action( 'wp_head', 'rsd_link' ); // Removes Really Simple Discovery link.
remove_action( 'wp_head', 'feed_links', 2 ); // Removes RSS feed links.
remove_action( 'wp_head', 'feed_links_extra', 3 ); // Removes all extra RSS feed links.
remove_action( 'wp_head', 'wlwmanifest_link' ); // Removes wlwmanifest.xml.
remove_action( 'wp_head', 'wp_resource_hints', 2 ); // Removes meta rel=dns-prefetch href=//s.w.org
remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0 ); // Removes relational links for the posts.
remove_action( 'wp_head', 'wp_oembed_add_discovery_links', 10 ); // Removes oEmbeds.
// Disable Post Formats
function ni_remove_post_formats() {
remove_theme_support('post-formats');
}
add_action( 'after_setup_theme', 'ni_remove_post_formats', 11 );
// Disable New User Registration Emails to Admin
// Code taken from Disable User Registration Notification Emails by Potent Plugins
add_action('init', 'pp_durne_init');
function pp_durne_init() {
// Unhook the actions from wp-includes/default-filters.php
remove_action('register_new_user', 'wp_send_new_user_notifications');
remove_action('edit_user_created_user', 'wp_send_new_user_notifications', 10, 2);
// Replace with our action that sends the user email only
add_action('register_new_user', 'pp_durne_send_notification');
add_action('edit_user_created_user', 'pp_durne_send_notification', 10, 2);
}
function pp_durne_send_notification($userId, $to='both') {
if (empty($to) || $to == 'admin') {
// Admin only, so we don't do anything
return;
}
// For 'both' or 'user', we notify only the user
wp_send_new_user_notifications($userId, 'user');
}
// Disable User Password Change Emails to Admin
if ( ! function_exists( 'wp_password_change_notification' ) ) {
function wp_password_change_notification( $user ) {
return;
}
}
// Sort Settings and Tools Menu Items Alphabetically
function ni_sort_settings_tools_menu_items_alphabetically() {
global $submenu;
$menus_to_sort = array(
'options-general.php', // Settings menu
'tools.php' // Tools menu
);
foreach ($menus_to_sort as $menu_slug) {
// Check if the menu exists
if (isset($submenu[$menu_slug]) && is_array($submenu[$menu_slug])) {
// Sort the items under the menu alphabetically
usort($submenu[$menu_slug], function ($a, $b) use ($menu_slug) {
// Ensure both array elements have the 0-index set and are strings
if (isset($a[0]) && isset($b[0]) && is_string($a[0]) && is_string($b[0])) {
// Prioritize 'General' item if on Settings menu
if ($menu_slug === 'options-general.php') {
if (isset($a[2]) && $a[2] === $menu_slug) {
return -1;
}
if (isset($b[2]) && $b[2] === $menu_slug) {
return 1;
}
}
return strcasecmp($a[0], $b[0]);
}
return 0;
});
// Update the href of the first link to go to the corresponding page
if (isset($submenu[$menu_slug][0]) && is_array($submenu[$menu_slug][0])) {
$submenu[$menu_slug][0][2] = $menu_slug;
}
}
}
}
add_action('admin_menu', 'ni_sort_settings_tools_menu_items_alphabetically', 999);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment