Archives: Snippets | Page 3

Disable WordPress Automatic Updates

WordPress automatically updates minor versions. If you prefer to control updates manually, add the code. For more on disabling automatic updates.

define( 'AUTOMATIC_UPDATER_DISABLED', true );
add_filter( 'auto_update_core', '__return_false' );
add_filter( 'auto_update_plugin', '__return_false' );
add_filter( 'auto_update_theme', '__return_false' );

Add lines 2-4 to functions.php. Add line 1 to wp-config.php.

Remove Shortlink, RSD and wlwmanifest from Header

WordPress adds meta tags for shortlink, RSD (Really Simple Discovery), and wlwmanifest (Windows Live Writer). Most sites do not need them. Add to your functions.php file. For more on securing WordPress.

remove_action( 'wp_head', 'wp_shortlink_wp_head' );
remove_action( 'wp_head', 'rsd_link' );
remove_action( 'wp_head', 'wlwmanifest_link' );
remove_action( 'wp_head', 'wp_generator' );

Remove jQuery Migrate

WordPress loads jQuery Migrate for backward compatibility. If your site and plugins support modern jQuery, you can remove Migrate and save an HTTP request. Add to your functions.php file. For more on performance optimization.

add_action( 'wp_default_scripts', function( $scripts ) {
	if ( ! is_admin() && isset( $scripts->registered['jquery'] ) ) {
		$script = $scripts->registered['jquery'];
		if ( $script->deps ) {
			$script->deps = array_diff( $script->deps, array( 'jquery-migrate' ) );
		}
	}
} );

Add Custom Image Sizes

You can define custom image sizes for your site. Images are generated on upload or when regenerating thumbnails. Add to your functions.php file. For more on image optimization.

add_action( 'after_setup_theme', function() {
	add_image_size( 'card-thumbnail', 400, 300, true );
	add_image_size( 'hero-banner', 1920, 600, true );
} );

Replace names and dimensions as needed. Use true for crop or false for scale.

Change Posts Per Page in Archives

WordPress defaults to 10 posts per page. You can change this for all archives or a specific one. Add to your functions.php file. For more on adding pagination.

add_action( 'pre_get_posts', function( $query ) {
	if ( ! is_admin() && $query->is_main_query() && $query->is_home() ) {
		$query->set( 'posts_per_page', 12 );
	}
} );

Replace is_home() with is_archive() or is_post_type_archive('your_cpt') as needed.

Remove WordPress Admin Footer Text

WordPress displays “Thank you for creating with WordPress” at the bottom of the admin. You can remove or replace the text. Add to your functions.php file. For more on WordPress hooks.

add_filter( 'admin_footer_text', '__return_empty_string' );
add_filter( 'update_footer', '__return_empty_string', 11 );

Remove Version Query String from Scripts and Styles

WordPress adds ?ver=X.X to CSS and JS files. Removing the version parameter can improve caching. Add to your functions.php file. For more tips on removing scripts and styles.

add_filter( 'script_loader_src', function( $src ) {
	return remove_query_arg( 'ver', $src );
}, 15, 1 );
add_filter( 'style_loader_src', function( $src ) {
	return remove_query_arg( 'ver', $src );
}, 15, 1 );

Disable XML-RPC in WordPress

XML-RPC allows remote connections to your WordPress site (e.g. from the app or for Pingbacks). Most sites do not need it, and it can be exploited. To disable, add to your functions.php file. For more tips on securing your WordPress site.

add_filter( 'xmlrpc_enabled', '__return_false' );

Restrict REST API to Logged-In Users Only

The WordPress REST API is public by default. If you do not need public access, restrict it to logged-in users. Add to your functions.php file. For more ways to secure the WordPress REST API.

add_filter( 'rest_authentication_errors', function( $result ) {
	if ( ! is_user_logged_in() ) {
		return new WP_Error( 'rest_not_logged_in', 'You must be logged in.', array( 'status' => 401 ) );
	}
	return $result;
} );