Archives: Snippets | Page 6

Disable Login Error Messages in WordPress

When users enter an incorrect username or password, an error message appears telling them which one is wrong. If someone is trying to break into your site by guessing one of these, that error message can help them understand what they got wrong. Block this by adding the following line to your functions.php file. For more security tips, see hardening your WordPress site:

add_filter('login_errors',create_function('$a', "return null;"));

Display Time Since Post Was Published in WordPress

A small snippet from the WordPress Codex that displays the time since a post was published.
Add the following code to your functions.php file:


/**
 * Display time since post was published
 *
 * @uses	human_time_diff()  Return time difference in easy to read format
 * @uses	get_the_time()  Get the time the post was published
 * @uses	current_time()  Get the current time
 *
 * @return	string  Timestamp since post was published
 *
 */
function get_time_since_posted() {

	$time_since_posted = 'Posted ' . human_time_diff( get_the_time( 'U' ), current_time( 'timestamp' ) ) . ' ago';

	return $time_since_posted;

}

Then add the function inside the loop where you want it:

echo get_time_since_posted();

For another useful post meta display, see Display Estimated Post Reading Time.

Set Custom Excerpt Word Count in WordPress

A simple function that sets the number of words returned when you call the the_excerpt function in WordPress. Add to functions.php and change the number on line 4 as needed.


function custom_excerpt_length( $length ) {
	return 40;
}
add_filter( 'excerpt_length', 'custom_excerpt_length', 999 );

This uses the excerpt_length filter hook. Learn more in WordPress Hooks Explained.

Remove the Submit Button in Gravity Forms

Gravity Forms is an excellent plugin for adding forms to your WordPress site. It lets you do this easily and quickly and includes many options for functional changes to forms.

For some reason, the plugin does not allow removing the Submit button from its forms, so if you want to use it and embed your own button you will need to remove the plugin’s default button. Add the following line to your functions.php file:

add_filter( 'gform_submit_button', '__return_false' );

For more form customization tips, see Restrict Free Emails in Forms.

Disable HTML in WordPress Comments

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' );

Disable Post Revisions in WordPress

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.

Hide the Admin Bar for Logged-In Users in WordPress

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.

Disable Comments in WordPress

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.

Remove Product Reviews in WooCommerce

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.

Redirect to Maintenance Page in WordPress

There are several plugins that show visitors a temporary message that your WordPress site is in maintenance mode, such as this plugin. It is a great plugin when you are developing the site or performing a temporary update, but these plugins will not help if your site goes down for any reason – they only work when the site is running properly. For when WordPress gets stuck in maintenance mode during updates, see how to fix WordPress maintenance mode.

In such cases it is worth preparing a permanent maintenance page on your server and redirecting visitors to it when your site goes down.

Add the following lines to your .htaccess file. Note that you need to change the address on line 3 to your IP so you can still access the site. Also change the page name and location according to your setup on line 3.

RewriteEngine on
RewriteCond %{REQUEST_URI} !/maintenance.html$
RewriteCond %{REMOTE_ADDR} !^123.123.123.123
RewriteRule $ /maintenance.html [R=302,L]