What is a Wordpress snippet and why use it?
Snippets in WordPress are short pieces of PHP code that allow you to add or modify functionality without installing a full plugin. They are ideal for developers and website administrators who want more control over how their site works, improve performance, increase security, or customize the admin interface.
Below is a selection of the most commonly used snippets, grouped by purpose – each with ready-to-copy code examples.
Security Snippets
Snippets focused on increasing WordPress security.
1. Disable XML-RPC
Protects the site from brute-force and overload attacks.
add_filter('xmlrpc_enabled', '__return_false');
2. Hide WordPress version from HTML header
Prevents attackers from identifying your WordPress version.
remove_action('wp_head', 'wp_generator');
3. Disable REST API for non-logged-in users
add_filter('rest_authentication_errors', function( $result ) {
if ( ! is_user_logged_in() ) {
return new WP_Error( 'rest_disabled', 'REST API is available to logged-in users only.', array( 'status' => 401 ) );
}
return $result;
});
WooCommerce Snippets
Snippets for customizing your WooCommerce store.
1. Change the “Add to cart” button text
add_filter('woocommerce_product_add_to_cart_text', function() {
return 'Buy Now';
});
2. Redirect to checkout after adding a product to cart
add_filter('woocommerce_add_to_cart_redirect', function() {
return wc_get_checkout_url();
});
3. Hide order notes field during checkout
add_filter( 'woocommerce_enable_order_notes_field', '__return_false' );
Admin Customization Snippets
1. Redirect user to a specific page after login
add_filter('login_redirect', function($redirect_to, $request, $user) {
return home_url('/my-dashboard/');
}, 10, 3);
2. Hide update notifications for non-admin users
if (!current_user_can('manage_options')) {
remove_action('admin_notices', 'update_nag', 3);
}
3. Disable plugin update notifications
if (!current_user_can('update_plugins')) {
add_filter('pre_site_transient_update_plugins', '__return_null');
}
Performance Snippets
1. Disable emoji scripts
remove_action( 'wp_head', 'print_emoji_detection_script', 7 );
remove_action( 'wp_print_styles', 'print_emoji_styles' );
2. Remove oEmbed scripts (if not needed)
remove_action( 'wp_head', 'wp_oembed_add_discovery_links' );
remove_action( 'wp_head', 'wp_oembed_add_host_js' );
Cleanup and Optimization
1. Remove unused dashboard widgets
add_action('admin_menu', function() {
remove_meta_box('dashboard_quick_press', 'dashboard', 'side');
remove_meta_box('dashboard_primary', 'dashboard', 'side');
});
2. Disable automatic post revisions
add_action('admin_init', function() {
remove_action('post_updated', 'wp_save_post_revision');
});
How to add snippets correctly
You can add snippets in one of the following ways:
functions.php
file of your active theme (recommended for small customizations only).
Snippets are a fast and efficient way to customize WordPress without overloading your site with plugins. With the right code snippets, you can improve performance, enhance security, streamline admin workflows, and deliver a better experience to both visitors and users. Just a few lines of code can make your WordPress site faster, cleaner, and more powerful.