Truncate WordPress SEO breadcrumb length

Yoast’s SEO Plugin for WordPress is great. It’s the only SEO plugin I’ve used for years. However there is no control over the length of breadcrumbs it will create. Imagine a long page title breaking your design as the area dedicated to breadcrumbs is notoriously small and narrow.

Here’s how to limit the length in Yoast’s WordPress SEO plugin.

add_filter('wp_seo_get_bc_title', 'truncate_bc_title');
function truncate_bc_title() {
	$title = get_the_title();
 
	$title_length = strlen( $title );
	$limit = 30;
	$truncated = substr( $title, 0, $limit );
 
	if ( $title_length > $limit ) {
		$truncated .= '...';
	}
 
	$link['text'] = $truncated;
 
	return $link['text'];
}

[Snippet] Adding a class to get_gravatar

WordPress’ get_gravatar seems very neglected to most other functions. No $args, no $classes. No anything useful.
Luckily we can add a filter to add extra classes to get_gravatar.

function add_class_to_gravatar($class) {
    $class = str_replace("class='avatar", "class='avatar new-class", $class);
    return $class;
}
add_filter('get_avatar','add_class_to_gravatar');

 

[Snippet] Set a maximum number of WordPress post revisions to avoid database bloat.

Drop the following into your wp-config.php file to tell WordPress that we are only interested in the last 5 revisions. It will speed up the WP admin and avoid your database bloating too much with old, useless revisions.

if (!defined('WP_POST_REVISIONS')) define('WP_POST_REVISIONS', 5);

Hide WordPress plugin update notifications for non-admin users

WordPress plugin update notifications can be annoying. They can also be confusing for less technical customers, even worse, customers kept phoning me saying “WordPress says it wants to update, but it won’t let me – it’s broke”. No, it’s not broke, you don’t have permissions. I needed a way to hide plugin update notices for non-admin users.

global $user_login;
get_currentuserinfo();
if (!current_user_can('update_plugins')) { // checks to see 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;" ) );
}

[Snippet] Remove WooCommerce generator tags

Another  security through obscurity post. WooCommerce leaves a generator meta tag showing it’s version number – I don’t like that.
[codesyntax lang=”html4strict”]
<meta name=”generator” content=”WooCommerce 2.0.14″>

Remove XML-RPC when not in use for performance boost

Disabling XML-RPC on WordPress has a few advantages, speed and security. If you don’t use XML-RPC, get rid of it for  a faster, more secure WordPress install.

1. This will remove the XML-RPC HTML code.

 
add_filter( 'xmlrpc_enabled', '__return_false' );

2. You’ll also need remove the X-Pingback from the response headers too

add_filter( 'wp_headers', 'yourprefix_remove_x_pingback' );
function yourprefix_remove_x_pingback($headers){
    unset( $headers['X-Pingback'] );
    return $headers;
}

Remove WordPress plugin update notice for inactive plugins

WordPress plugin update notifications can be annoying. They can also be confusing for less technical customers. I needed a way to hide plugin update notices for plugins that aren’t enabled.

function update_active_plugins($value = '') {
    if ((isset($value-&gt;response)) &amp;&amp; (count($value-&gt;response))) {
 
        // Get the list cut current active plugins
        $active_plugins = get_option('active_plugins');    
        if ($active_plugins) {
 
            //  Here we start to compare the $value-&gt;response
            //  items checking each against the active plugins list.
            foreach($value-&gt;response as $plugin_idx =&gt; $plugin_item) {
 
                // If the response item is not an active plugin then remove it.
                // This will prevent WordPress from indicating the plugin needs update actions.
                if (!in_array($plugin_idx, $active_plugins))
                    unset($value-&gt;response[$plugin_idx]);
            }
        }
        else {
             // If no active plugins then ignore the inactive out of date ones.
            foreach($value-&gt;response as $plugin_idx =&gt; $plugin_item) {
                unset($value-&gt;response);
            }          
        }
    }  
    return $value;
}
add_filter('transient_update_plugins', 'update_active_plugins');

Overwrite Woocommerce JS

I recently had to change certain parts of the WooCommerce Javascript. It would be bad to edit the JS provided with the plugin as it would be overwritten by an upgrade. WooCommerce gives us an easy way of having custom templates but no matching, easy way of using custom Javascript.

I had to overwrite some functionality in the add-to-cart.js, so I created a new JS called /js/add-to-cart-kb-custom.js.
 This is how I did it in functions.php Continue reading “Overwrite Woocommerce JS”

[Snippet] Add parent page slug to body class

WordPress adds a huge range of styles to the body class. Sometimes that’s not enough. I wanted to add the post parent to the body class too for some specific styling. Here’s how it’s done.

add_filter('body_class','body_class_section');
 
function body_class_section($classes) {
    global $wpdb, $post;
    if (is_page()) {
        if ($post-&gt;post_parent) {
            $parent  = end(get_post_ancestors($current_page_id));
        } else {
            $parent = $post-&gt;ID;
        }
        $post_data = get_post($parent, ARRAY_A);
        $classes[] = 'section-' . $post_data['post_name'];
    }
    return $classes;
}