Everyone likes a good filter and virtually anything you want to do with WordPress has some sort of filter built into it.
So we can change the string appended to the_excerpt():
/**
* Add "… Continued" to the excerpt
*/
add_filter('excerpt_more', function () {
return '...';
});
Or change the number of words displayed by the_excerpt():
/**
* Change number of words displayed in the excerpt
*/
add_filter( 'excerpt_length', function ( $length ) {
return 42;
}, 999 );
Or add an edit button after the_content():
/**
* Add edit buttons to content
*/
add_filter('the_content', function ($content){
if(is_user_logged_in()){
$content .= '<p class="edit-this"><a href="' . get_edit_post_link() . '" class="edit-post">Edit This</a></p>';
}
return $content;
}, 1000);
Or change how some titles are rendered:
add_filter('get_the_archive_title', function($title){
if(is_category()) {
$title = single_cat_title('', false);
}
return $title;
});
(Deep breath)
Add custom image sizes to the post edit screens:
/**
* Add image size to thumbnail selection list in post edit screen.
* Thumbnail size must already be defined..
*/
add_filter( 'image_size_names_choose', function ( $sizes ) {
$sizes['square'] = 'Square';
return $sizes;
});
// Or
/**
* Add custom image size to media window
*/
add_filter('image_size_names_choose', function($sizes){
return array_merge( $sizes, ['square' => 'Square'] );
});
Perhaps, for woocommerce users, adding the qty of items in the basket to the menu would be useful:
/** add Cart contents to Basket Menu Item */
add_filter('nav_menu_item_title', function($title, $item, $args, $depth){
global $woocommerce;
$basket = get_option( 'woocommerce_cart_page_id' );
if($title == 'Cart' || $title == 'Basket') {
$qty = $woocommerce->cart->cart_contents_count;
if($qty > 0) {
$title .= '<span id="cart-contents" class="cart-contents">' . $qty . '</span>';
}
}
return $title;
}, 99, 4);
/** Show cart contents / total Ajax */
add_filter( 'woocommerce_add_to_cart_fragments', function ( $fragments ) {
global $woocommerce;
$qty = $woocommerce->cart->cart_contents_count;
ob_start();
if($qty > 0) { ?>
<span id="cart-contents" class="cart-contents"><?php echo $qty; ?></span>
<?php }
$fragments['span#cart-contents'] = ob_get_clean();
return $fragments;
} );
Another classic woocommerce filter, is for the Product attribute label for variations, this filter will change it to the attributes name:
/** Add label to product attribute select elements */
add_filter( 'woocommerce_dropdown_variation_attribute_options_args', function ( $args ) {
if(!is_admin()) {
$attr = get_taxonomy( $args['attribute'] );
$args['show_option_none'] = "Choose a " . $attr->labels->singular_name;
}
return $args;
}, 10 );
And removing the default behavouir for private posts (normally they have’private: ‘ prepended to the title):
/** remove "private" from Private page titles */
add_filter( 'private_title_format', function ( $format ) {
return "%s";
} );
And finally, adding extra columns to admin tables to show some kind of custom meta data:
/** filter to add custom column 'school' to the users table in dashboard. */
add_filter('manage_users_columns', function($columns){
$columns['school'] = 'School';
return $columns;
});
/** populates the 'school' column with a custom user meta value. */
add_filter('manage_users_custom_column', function($val, $column_name, $user_id){
if($column_name === 'school') {
$school = get_user_meta($user_id, '_school', true);
if(!empty($school)) {
return $school;
}
}
return $val;
}, 10, 3)
And there we have a few of my most used WordPress filter snippets! What are yours?