search

Archives: Snippets | Page 6

Change Add to Cart Button Text in WooCommerce

A few weeks ago I ran into a situation where a client wanted different text instead of the standard “Add to Cart” or “Add to Bag” in WooCommerce. Here is the code to add to functions.php to change the text:

add_filter( 'woocommerce_product_single_add_to_cart_text', 'woo_custom_cart_button_text' );
 
function woo_custom_cart_button_text() {
         return __( 'My Button Text', 'woocommerce' );
 }

Allow SVG File Uploads in WordPress Media Library

To allow SVG file uploads through the media library, add the following code to your functions.php file:

function cc_mime_types($mimes) {
  $mimes['svg'] = 'image/svg+xml';
  return $mimes;
}
add_filter('upload_mimes', 'cc_mime_types');

A more detailed explanation can be found in this post.

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.

Savvy WordPress Development official logo