There are situations (quite common) where you want to limit WordPress search to titles only and not the post body text. To do this, add the following code to your functions.php file. For a more powerful search engine, see Relevanssi Better Search. Note that this function also applies to search performed from the WordPress dashboard.
/**
* Search SQL filter for matching against post title only.
*
* @param string $search
* @param WP_Query $wp_query
*/
function wpse_11826_search_by_title( $search, $wp_query ) {
if ( ! empty( $search ) && ! empty( $wp_query->query_vars['search_terms'] ) ) {
global $wpdb;
$q = $wp_query->query_vars;
$n = ! empty( $q['exact'] ) ? '' : '%';
$search = array();
foreach ( ( array ) $q['search_terms'] as $term )
$search[] = $wpdb->prepare( "$wpdb->posts.post_title LIKE %s", $n . $wpdb->esc_like( $term ) . $n );
if ( ! is_user_logged_in() )
$search[] = "$wpdb->posts.post_password = ''";
$search = ' AND ' . implode( ' AND ', $search );
}
return $search;
}
add_filter( 'posts_search', 'wpse_11826_search_by_title', 10, 2 );
Contact Form 7 is a great tool for embedding forms in WordPress, but sometimes it adds <p> and <br> tags to the form. To disable this, add the following line to your wp-config.php file:
define('WPCF7_AUTOP', false );
More on the CF7 plugin in the post on styling the Contact Form 7 plugin.
There are several ways to add Google Fonts to a WordPress site. Here are two of them. The first and less recommended option is to add the fonts directly to your theme’s header.php (preferably in a child theme).
<link rel="stylesheet" type="text/css" href="http://fonts.googleapis.com/css?family=Lora|Oswald" media="screen">
When using this method, note that you must add the Google Fonts call before you load the main stylesheet.
The second and more recommended option is using wp_register_style and wp_enqueue_style. Add the following code to your functions.php file:
function add_my_google_fonts() {
wp_register_style('my-googleFonts', 'https://fonts.googleapis.com/css?family=Open+Sans:300italic,300,600,700,400');
wp_enqueue_style( 'my-googleFonts');
}
add_action('wp_enqueue_scripts', 'add_my_google_fonts');
Avoid making two or more calls to Google’s servers – you can embed all fonts in a single call.
There is also an option for local embedding of Google fonts. Local embedding has several advantages – for more on this, see the post Loading Google Fonts locally.
This function adds a class to the last post in the WordPress loop. Using it lets you style the last post differently on the homepage or archive pages.
function last_post_class($classes){
global $wp_query;
if(($wp_query->current_post+1) == $wp_query->post_count) $classes[] = 'last';
return $classes;
}
add_filter('post_class', 'last_post_class');
For more on WordPress template structure, see WordPress Template Hierarchy.
If you do not want short comments on your posts such as “Thanks!” or “Great article…“, add the following code to your functions.php file and set the minimum character count on line 4:
function minimal_comment_length( $commentdata ) {
$minimalCommentLength = 20;
if ( strlen( trim( $commentdata['comment_content'] ) ) < $minimalCommentLength ) {
wp_die( 'Your comment must contain at least ' . $minimalCommentLength . ' characters.' );
}
return $commentdata;
}
add_filter( 'preprocess_comment', 'minimal_comment_length' );
For more comment management, see Show the Total Number of Comments.
To hide a product category from your WooCommerce shop, add the following code to your functions.php file and replace xxxxxx with the slug of the category you want to hide:
function custom_pre_get_posts_query( $q ) {
if ( ! $q->is_main_query() ) return;
if ( ! $q->is_post_type_archive() ) return;
if ( ! is_admin() && is_shop() ) {
$q->set( 'tax_query', array(array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => array( 'xxxxxx' ),
'operator' => 'NOT IN'
)));
}
remove_action( 'pre_get_posts', 'custom_pre_get_posts_query' );
}
add_action( 'pre_get_posts', 'custom_pre_get_posts_query' );
For more on WooCommerce categories, see Check if a Product Belongs to a Specific Category.
The following code lets you change the logo on the WordPress login page as well as the link it points to:
add_filter( 'login_headerurl', 'namespace_login_headerurl' );
function namespace_login_headerurl( $url ) {
$url = home_url( '/' );
return $url;
}
function namespace_login_headertitle( $title ) {
$title = get_bloginfo( 'name' );
return $title;
}
add_filter( 'login_headertitle', 'namespace_login_headertitle' );
function namespace_login_style() {
echo '<style>.login h1 a { background-image: url( ' . get_template_directory_uri() . '/images/logo.png ) !important; }</style>';
}
add_action( 'login_head', 'namespace_login_style' );
For more login page security tips, see Hardening WordPress Security.
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.