The following code ensures that no user logged into the WordPress dashboard will see WordPress version update notices:
global $user_login;
get_currentuserinfo();
if ($user_login !== "admin") {
add_action( 'init', create_function( '$a', "remove_action( 'init', 'wp_version_check' );" ), 2 );
add_filter( 'pre_option_update_core', create_function( '$a', "return null;" ) );
}
Change “admin” to the username that should receive updates.
Alternative version that shows the update notice to all site administrators, not just the user named “admin”:
global $user_login;
get_currentuserinfo();
if (!current_user_can('update_plugins')) {
add_action( 'init', create_function( '$a', "remove_action( 'init', 'wp_version_check' );" ), 2 );
add_filter( 'pre_option_update_core', create_function( '$a', "return null;" ) );
}
This uses WordPress hooks. Learn more in WordPress Hooks Explained.
Every time you upload an image to the media library, WordPress saves it in several default sizes. Usually this is fine, but there are many cases where you do not need those images – for example if you use or define your own image sizes in your theme.
In such cases it makes sense to prevent WordPress from creating these images since they take up server space unnecessarily. To remove default image sizes in WordPress, add the following code to your functions.php file:
function wcr_remove_intermediate_image_sizes($sizes, $metadata) {
$disabled_sizes = array(
'thumbnail',
'medium',
'large'
);
foreach ($disabled_sizes as $size) {
if (!isset($sizes[$size])) {
continue;
}
unset($sizes[$size]);
}
return $sizes;
}
add_filter('intermediate_image_sizes_advanced', 'wcr_remove_intermediate_image_sizes', 10, 2);
For a broader guide on this topic, see the article on image sizes in WordPress.
There are situations where the “Website” (or URL) field is not needed in the comment form. In such cases, remove the field by adding the following code to your functions.php file:
/**
* Remove the URL field
*
* @param array $fields
*
* @return array
*/
function sv_comment_form_default_fields( $fields ) {
if ( isset( $fields['url'] ) ) {
unset( $fields['url'] );
}
return $fields;
}
add_filter( 'comment_form_default_fields', 'sv_comment_form_default_fields' );
If you use a child theme, add this code to your child theme’s functions.php file.
WordPress generates several image sizes according to your theme settings. These images are created at quality 90 on a scale of 1-100 by default. You can save bandwidth by changing this setting to 75 for more aggressive compression.
/**
* Snippet Name: Change jpg compression level
*/
function sv_jpeg_quality() {
return 75;
}
add_filter( 'jpeg_quality', 'sv_jpeg_quality' );
For more on optimizing images for WordPress, see optimizing WordPress images for SEO.
We use a filter called pre_get_posts to modify the main loop and tell it to ignore sticky posts. Note that this will cause them not to appear anywhere when you call the loop. If you want to display them, you will need to write your own loop above the main loop that only queries those sticky posts:
/**
* Snippet Name: Remove sticky posts from the main query
*/
function sv_ignore_sticky( $query ) {
if ( is_home() && $query->is_main_query() ) {
$query->set( 'ignore_sticky_posts', true );
}
$query->set( 'post__not_in', get_option( 'sticky_posts' ) );
}
add_action( 'pre_get_posts', 'sv_ignore_sticky' );
Sometimes you need to add a specific attribute to an image. You can do this quickly using the the_post_thumbnail function. The following snippet adds the itemprop="image" attribute to images, but you can also use it to add class, alt, title, etc.
the_post_thumbnail( 'thumbnail', array('itemprop'=>'image') );
For more on WordPress images, see Image Sizes in WordPress.
When you write a post, it is automatically saved as a draft every minute. If your browser crashes this lets you recover your work. You can change the time between saves by adding the following line to your wp-config.php file:
define( 'AUTOSAVE_INTERVAL', X);
Where X is the number of seconds between saves.
For more wp-config.php settings, see Optimize WordPress Configuration via wp-config.php.
How to change the position of the admin bar in WordPress?
More than once the WordPress admin bar has bothered me because it hid menus that were set to position:fixed in CSS. A few lines in functions.php and the admin bar moves to the bottom of the page:
<?php
function fb_move_admin_bar() { ?>
<style type="text/css">
body {
margin-top: -28px;
padding-bottom: 28px;
}
body.admin-bar #wphead {
padding-top: 0;
}
body.admin-bar #footer {
padding-bottom: 28px;
}
#wpadminbar {
top: auto !important;
bottom: 0;
}
#wpadminbar .quicklinks .menupop ul {
bottom: 28px;
}
</style>
<?php }
add_action( 'admin_head', 'fb_move_admin_bar' );
add_action( 'wp_head', 'fb_move_admin_bar' );
For safe WordPress customizations, see What Are Child Themes and How to Use Them.
The Sitemap file helps search engines index your site and find the most important pages easily. If you do not want the Sitemap to appear in Google search results, add the following lines to the .htaccess file in the root directory where WordPress is installed:
<IfModule mod_rewrite.c>
<Files sitemap.xml>
Header set X-Robots-Tag "noindex"
</Files>
</IfModule>
Replace sitemap.xml with your Sitemap file name. For more on what a Sitemap is, see the post How to create a Sitemap in WordPress and how to submit it via Google Search Console.
You want search engines to index your blog pages but not the PHP files of your WordPress installation. Edit the robots.txt file and add the following lines to block search engines from indexing these files:
User-agent: *
Disallow: /wp-admin/
Disallow: /wp-includes/
Disallow: /wp-content/plugins/
Disallow: /wp-content/themes/
Disallow: /feed/
Disallow: */feed/
For more on managing search engine access, see Create and Send a Sitemap for WordPress SEO.