Skip to content

Instantly share code, notes, and snippets.

@mcascardi
Last active March 9, 2023 06:35
Show Gist options
  • Save mcascardi/76dbd0d736193ab207909c7a3b6d840e to your computer and use it in GitHub Desktop.
Save mcascardi/76dbd0d736193ab207909c7a3b6d840e to your computer and use it in GitHub Desktop.
A collection of security configurations for WordPress
<?php
// Remove links for blog clients, shortlink, and generator info from header
remove_action('wp_head', 'rsd_link');
remove_action('wp_head', 'wlwmanifest_link');
remove_action('wp_head', 'wp_shortlink_wp_head');
remove_action('wp_head', 'wp_generator');
// Remove Yoast SEO next/prev links
add_filter('wpseo_prev_rel_link', '__return_empty_string' );
add_filter('wpseo_next_rel_link', '__return_empty_string' );
// Remove Yoast Comments
if (defined('WPSEO_VERSION')){
add_action('get_header',function (){ ob_start(function ($o){
return preg_replace('/^<!--.*?[Y]oast.*?-->$/mi','',$o); }); });
add_action('wp_head',function (){ ob_end_flush(); }, 999);
}
// Disable XMLRPC
add_filter('xmlrpc_enabled', '__return_false');
// Disable wp-json users
add_filter( 'rest_endpoints', function( $endpoints ){
if ( isset( $endpoints['/wp/users'] ) ) {
unset( $endpoints['/wp/users'] );
}
if ( isset( $endpoints['/wp/users/(?P<id>[\d]+)'] ) ) {
unset( $endpoints['/wp/users/(?P<id>[\d]+)'] );
}
return $endpoints;
});
function dont_track_pages() {
global $gtm4wp_options;
if (is_page('annual-general-meeting')) {
// Replaces user data lookup with noop function
echo '<script>WPGLOBALJS.ajaxGetUserData = function() {};</script>';
// Stops Google Tag Manager script from being rendered
unset($gtm4wp_options[ GTM4WP_OPTION_GTM_CODE ]);
}
}
add_action('wp_head', 'dont_track_pages');
function delete_all_my_cookies() {
if ($_SERVER['REQUEST_URI'] == '/special-url/no-cookies-allowed/') {
// unset cookies
if (isset($_SERVER['HTTP_COOKIE'])) {
$cookies = explode(';', $_SERVER['HTTP_COOKIE']);
// echo $cookies;
foreach($cookies as $cookie) {
$parts = explode('=', $cookie);
$name = trim($parts[0]);
setcookie($name, '', time()-1000);
setcookie($name, '', time()-1000, '/');
}
}
}
}
add_action('init', 'delete_all_my_cookies');
function remove_feed_links()
{
remove_action( 'wp_head', 'feed_links_extra', 3 ); // Display the links to the extra feeds such as category feeds
remove_action( 'wp_head', 'feed_links', 2 ); // Display the links to the general feeds: Post and Comment Feed
remove_action( 'wp_head', 'rsd_link' ); // Display the link to the Really Simple Discovery service endpoint, EditURI link
remove_action( 'wp_head', 'wlwmanifest_link' ); // Display the link to the Windows Live Writer manifest file.
remove_action( 'wp_head', 'index_rel_link' ); // index link
remove_action( 'wp_head', 'parent_post_rel_link', 10, 0 ); // prev link
remove_action( 'wp_head', 'start_post_rel_link', 10, 0 ); // start link
remove_action( 'wp_head', 'adjacent_posts_rel_link', 10, 0 ); // Display relational links for the posts adjacent to the current post.
remove_action( 'wp_head', 'wp_generator' ); // Display the XHTML generator that is generated on the wp_head hook, WP version
}
add_action('init', 'remove_feed_links');
function disable_wp_emojicons()
{
// all actions related to emojis
remove_action( 'admin_print_styles', 'print_emoji_styles' );
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_filter( 'wp_mail', 'wp_staticize_emoji_for_email' );
remove_filter( 'the_content_feed', 'wp_staticize_emoji' );
remove_filter( 'comment_text_rss', 'wp_staticize_emoji' );
// filter to remove TinyMCE emojis
add_filter( 'tiny_mce_plugins', 'disable_emojicons_tinymce' );
}
add_action( 'init', 'disable_wp_emojicons' );
// We will need the following filter function to disable TinyMCE emojicons:
function disable_emojicons_tinymce( $plugins )
{
if ( is_array( $plugins ) ) {
return array_diff( $plugins, array( 'wpemoji' ) );
} else {
return array();
}
}
// Remove links to oembed from the head
remove_action('wp_head', 'wp_oembed_add_discovery_links');
remove_action('wp_head', 'wp_oembed_add_host_js');
remove_action('wp_head', 'rest_output_link_wp_head', 10, 0);
function acf_safety_functions() {
if (!function_exists('get_field')) { function get_field() { return; } }
if (!function_exists('the_field')) { function the_field() { return; } }
if (!function_exists('get_sub_field')) { function get_sub_field() { return; } }
if (!function_exists('the_sub_field')) { function the_sub_field() { return; } }
if (!function_exists('have_rows')) { function have_rows() { return false; } }
}
add_action('wp_head', 'acf_safety_functions');
// EOF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment