Last active
December 13, 2020 09:18
-
-
Save kosmala007/051661a1050015327f05474dbde91f91 to your computer and use it in GitHub Desktop.
Usefull actions for wordpres eg, remove version rom source, remove emojis scripts, enable recaptcha or contact form 7 only on specyfic pages
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Change default WP login errors message. | |
*/ | |
add_filter('login_errors', function (): string { | |
return 'Invalid credentials ¯\_(ツ)_/¯'; | |
}); | |
/** | |
* Hide WP version from source code. | |
*/ | |
function remove_version_scripts_styles(string $src): string | |
{ | |
if (strpos($src, 'ver=')) { | |
$src = remove_query_arg('ver', $src); | |
} | |
return $src; | |
} | |
remove_action('wp_head', 'wp_generator'); | |
add_filter('the_generator', '__return_empty_string'); | |
add_filter('style_loader_src', 'remove_version_scripts_styles', 9999); | |
add_filter('script_loader_src', 'remove_version_scripts_styles', 9999); | |
/** | |
* Load Contact Form 7 js and css on specific pages. | |
*/ | |
add_action('wp_print_scripts', function () { | |
if (!is_page(10)) { | |
wp_deregister_script('contact-form-7'); | |
} | |
}, 9999); | |
add_action('wp_print_styles', function () { | |
if (!(is_page('page id'))) { | |
wp_deregister_style('contact-form-7'); | |
} | |
}, 9999); | |
/** | |
* Remove Google ReCaptcha code/badge everywhere apart from select pages. | |
*/ | |
add_action('wp_print_scripts', function () { | |
if (!(is_page('page id'))) { | |
wp_dequeue_script('google-recaptcha'); | |
wp_dequeue_script('google-invisible-recaptcha'); | |
} | |
}); | |
/** | |
* Remove Emojis. | |
*/ | |
add_action('init', function ($plugins) { | |
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'); | |
// remove from TinyMCE | |
add_filter('tiny_mce_plugins', function ($plugins) { | |
if (is_array($plugins)) { | |
return array_diff($plugins, array('wpemoji')); | |
} else { | |
return array(); | |
} | |
}); | |
}); | |
/** | |
* Remove wp-embed.js | |
*/ | |
add_action('wp_footer', function () { | |
wp_deregister_script('wp-embed'); | |
}); | |
/** | |
* Remove jQuery. | |
*/ | |
add_action('wp_enqueue_scripts', function () { | |
wp_deregister_script('jquery'); | |
}); | |
/** | |
* Remove gutenger styles. | |
*/ | |
add_action('wp_print_styles', function () { | |
wp_dequeue_style('wp-block-library'); | |
}); | |
/** | |
* Remove jQuery migrate. | |
*/ | |
add_filter('wp_default_scripts', function ($scripts) { | |
if (!is_admin()) { | |
$scripts->remove('jquery'); | |
$scripts->add('jquery', false, array('jquery-core'), '1.2.1'); | |
} | |
}); | |
/** | |
* Remove of styles and scripts (WooCommerce) when not in use. | |
*/ | |
add_action( 'wp_enqueue_scripts', function() { | |
if(function_exists('is_woocommerce') | |
&& (!is_woocommerce() && !is_cart() && !is_checkout()) | |
) { | |
wp_dequeue_style('woocommerce-layout'); | |
wp_dequeue_style('woocommerce-general'); | |
wp_dequeue_style('woocommerce-smallscreen'); | |
wp_dequeue_style('woocommerce-inline'); | |
wp_dequeue_style('wc-block-style'); | |
wp_dequeue_script('wc-cart-fragments'); | |
wp_dequeue_script('woocommerce'); | |
wp_dequeue_script('wc-add-to-cart'); | |
wp_dequeue_script('cart-widget'); | |
wp_dequeue_script('wcml-front-scripts'); | |
} | |
}, 9999); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment