Skip to content

Instantly share code, notes, and snippets.

@danfoust

danfoust/actions.php

Last active Nov 21, 2020
Embed
What would you like to do?
Useful Wordpress setup
add_action( 'init', function() {
// Remove the REST API endpoint.
remove_action('rest_api_init', 'wp_oembed_register_route');
// Turn off oEmbed auto discovery.
// Don't filter oEmbed results.
remove_filter('oembed_dataparse', 'wp_filter_oembed_result', 10);
// Remove oEmbed discovery links.
remove_action('wp_head', 'wp_oembed_add_discovery_links');
// Remove oEmbed-specific JavaScript from the front-end and back-end.
remove_action('wp_head', 'wp_oembed_add_host_js');
}, PHP_INT_MAX - 1 );
// REMOVE WP EMOJI
remove_action('wp_head', 'print_emoji_detection_script', 7);
remove_action('wp_print_styles', 'print_emoji_styles');
remove_action( 'admin_print_scripts', 'print_emoji_detection_script' );
remove_action( 'admin_print_styles', 'print_emoji_styles' );
<?php
/**
* Plugin Name: Disable Comments
* Plugin URI: https://gist.github.com/danfoust/72742429a74d5a691cd1f2db064ce620
* Description: A must-use plugin that disables comments everywhere
* Version: 1.0.0
* Author: Daniel Foust
* Author URI: https://foustwebdesignllc.com
* License: MIT License
*/
// Disable support for comments and trackbacks in post types
add_action('admin_init', function () {
$post_types = get_post_types();
foreach ($post_types as $post_type) {
if (post_type_supports($post_type, 'comments')) {
remove_post_type_support($post_type, 'comments');
remove_post_type_support($post_type, 'trackbacks');
}
}
});
// Close comments on the front-end
add_filter('comments_open', function () {
return false;
}, 20, 2);
add_filter('pings_open', function () {
return false;
}, 20, 2);
// Hide existing comments
add_filter('comments_array', function ($comments) {
$comments = array();
return $comments;
}, 10, 2);
// Remove comments page in menu
add_action('admin_menu', function () {
remove_menu_page('edit-comments.php');
});
// Redirect any user trying to access comments page
add_action('admin_init', function () {
global $pagenow;
if ($pagenow === 'edit-comments.php') {
wp_redirect(admin_url());
exit;
}
});
// Remove comments metabox from dashboard
add_action('admin_init', function () {
remove_meta_box('dashboard_recent_comments', 'dashboard', 'normal');
});
// Remove comments links from admin bar
add_action('init', function () {
if (is_admin_bar_showing()) {
remove_action('admin_bar_menu', 'wp_admin_bar_comments_menu', 60);
}
});
// Disable Attachment Pages Completely
add_filter('rewrite_rules_array', function ($rules) {
foreach ($rules as $regex => $query) {
if (strpos($regex, 'attachment') || strpos($query, 'attachment')) {
unset($rules[ $regex ]);
}
}
return $rules;
});
// Remove admin attachment page pretty URLs in media library
add_filter('attachment_link', function () {
return;
});
/**
* Disable jQuery Migrate in WordPress.
*
* @author Guy Dumais.
* @link https://en.guydumais.digital/disable-jquery-migrate-in-wordpress/
*/
add_filter('wp_default_scripts', function (&$scripts) {
if (!is_admin()) {
$scripts->remove('jquery');
$scripts->add('jquery', false, array( 'jquery-core' ), '1.12.4');
}
}, PHP_INT_MAX);
/**
* Use Lozad (lazy loading) for attachments/featured images
*/
add_filter('wp_get_attachment_image_attributes', function ($attr, $attachment) {
// Bail on admin
if (is_admin()) {
return $attr;
}
$attr['data-src'] = $attr['src'];
$attr['class'] .= ' lozad';
unset($attr['src']);
return $attr;
}, 10, 2);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment