By default WordPress allows adding HTML tags in comments such as outbound links, various tags, etc.
Comments do contain rel=nofollow on links automatically, but if you want to completely disable the ability to add any HTML in comments, add the following line to your functions.php file:
add_filter( 'pre_comment_content', 'esc_html' );
WordPress includes functionality that lets you track changes to posts and restore previous versions (Post Revisions). However, these increase the size of the wp_posts table in the database since each revision is another row.
To disable the revision saving functionality in WordPress, add the following line to your wp-config.php file:
define( 'WP_POST_REVISIONS', false);
Alternatively, if you only want to limit the number of revisions saved, add:
define( 'WP_POST_REVISIONS', X);
Replace X with the number of revisions you want to keep.
A very annoying setting – WordPress displays the admin bar to all users logged into the site with their wordpress.com account. To remove the admin bar, add the following line to your functions.php file:
add_filter('show_admin_bar', '__return_false');
This uses a WordPress filter. Learn more about hooks in WordPress Hooks Explained.
WordPress has a built-in comment system that works great. However, in some cases we want to completely disable comments on the site, especially if your site is not a blog but a corporate or store site.
For some of you it is enough to disable this via Discussion settings in the WordPress dashboard, but sometimes that is not enough. Here is code to disable comments on WordPress sites.
This code completely disables the comment system in WordPress and also removes comments from the dashboard. Add the following code to your functions.php file:
function sv_disable_comments_post_types_support() {
$post_types = get_post_types();
foreach ($post_types as $post_type) {
if(post_type_supports($post_type, 'comments')) {
remove_post_type_support($post_type, 'comments');
remove_post_type_support($post_type, 'trackbacks');
}
}
}
add_action('admin_init', 'sv_disable_comments_post_types_support');
function sv_disable_comments_status() {
return false;
}
add_filter('comments_open', 'sv_disable_comments_status', 20, 2);
add_filter('pings_open', 'sv_disable_comments_status', 20, 2);
function sv_disable_comments_hide_existing_comments($comments) {
$comments = array();
return $comments;
}
add_filter('comments_array', 'sv_disable_comments_hide_existing_comments', 10, 2);
function sv_disable_comments_admin_menu() {
remove_menu_page('edit-comments.php');
}
add_action('admin_menu', 'sv_disable_comments_admin_menu');
function sv_disable_comments_admin_menu_redirect() {
global $pagenow;
if ($pagenow === 'edit-comments.php') {
wp_redirect(admin_url()); exit;
}
}
add_action('admin_init', 'sv_disable_comments_admin_menu_redirect');
function sv_disable_comments_dashboard() {
remove_meta_box('dashboard_recent_comments', 'dashboard', 'normal');
}
add_action('admin_init', 'sv_disable_comments_dashboard');
function sv_disable_comments_admin_bar() {
if (is_admin_bar_showing()) {
remove_action('admin_bar_menu', 'wp_admin_bar_comments_menu', 60);
}
}
add_action('init', 'sv_disable_comments_admin_bar');
This snippet uses WordPress hooks. Learn more in WordPress Hooks Explained.
By default, the WooCommerce product page shows a “Reviews” tab that lets visitors leave reviews. If you want to remove the review system in WooCommerce on your WordPress site, add the following code to your functions.php file:
add_filter( 'woocommerce_product_tabs', 'sb_woo_remove_reviews_tab', 98);
function sb_woo_remove_reviews_tab($tabs) {
unset($tabs['reviews']);
return $tabs;
}
For more product page customization, see Edit Product Data Tabs in WooCommerce.
When working on a project that requires creating multiple taxonomies, it makes sense to use this function rather than creating a separate function for each taxonomy. Put each taxonomy’s parameters in the array and the function handles the rest:
<?php
/**
* Register Multiple Taxonomies
*
*/
function register_multiple_taxonomies() {
$taxonomies = array(
array(
'slug' => 'job-department',
'single_name' => 'Department',
'plural_name' => 'Departments',
'post_type' => 'jobs',
'rewrite' => array( 'slug' => 'department' ),
),
array(
'slug' => 'job-type',
'single_name' => 'Type',
'plural_name' => 'Types',
'post_type' => 'jobs',
'hierarchical' => false,
),
array(
'slug' => 'job-experience',
'single_name' => 'Min-Experience',
'plural_name' => 'Min-Experiences',
'post_type' => 'jobs',
),
);
foreach( $taxonomies as $taxonomy ) {
$labels = array(
'name' => $taxonomy['plural_name'],
'singular_name' => $taxonomy['single_name'],
'search_items' => 'Search ' . $taxonomy['plural_name'],
'all_items' => 'All ' . $taxonomy['plural_name'],
'parent_item' => 'Parent ' . $taxonomy['single_name'],
'parent_item_colon' => 'Parent ' . $taxonomy['single_name'] . ':',
'edit_item' => 'Edit ' . $taxonomy['single_name'],
'update_item' => 'Update ' . $taxonomy['single_name'],
'add_new_item' => 'Add New ' . $taxonomy['single_name'],
'new_item_name' => 'New ' . $taxonomy['single_name'] . ' Name',
'menu_name' => $taxonomy['plural_name']
);
$rewrite = isset( $taxonomy['rewrite'] ) ? $taxonomy['rewrite'] : array( 'slug' => $taxonomy['slug'] );
$hierarchical = isset( $taxonomy['hierarchical'] ) ? $taxonomy['hierarchical'] : true;
register_taxonomy( $taxonomy['slug'], $taxonomy['post_type'], array(
'hierarchical' => $hierarchical,
'labels' => $labels,
'show_ui' => true,
'query_var' => true,
'rewrite' => $rewrite,
));
}
}
add_action( 'init', 'register_multiple_taxonomies' );
In my last project I needed to split the WordPress loop into groups of three posts – so if there are nine posts, every three would be wrapped in a div. You can do this using a counter and checking inside the loop whether it is a multiple of 3, and if so, closing the div. To change the number of posts per group, change the number 3 on line 14. Here is the code:
<?php if ( have_posts() ) :
$i = 0;
while ( have_posts() ) : the_post();
if ( $i % 3 == 0 ) { ?>
<div>
<?php } ?>
<div class="single_item">
<h2>
<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?>
</a>
</h2>
</div>
<?php $i ++;
if ( $i % 3 == 0 ) { ?>
</div>
<?php } ?>
<?php endwhile; ?>
<?php
if ( $i % 3 != 0 ) { ?>
</div>
<?php } ?>
<?php endif; ?>
For more loop customization, see Remove a Category from the Main Loop.
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.