Skip to content

Instantly share code, notes, and snippets.

@FriendlyWP
FriendlyWP / functions.php
Created November 18, 2013 20:33
Limit words in excerpts
/***
**** LIMIT WORDS IN EXCERPTS (or any supplied content)
Usage:
$text = get_the_excerpt();
echo string_limit_words($text, 40);
*/
function string_limit_words($string, $word_limit) {
$words = explode(' ', $string, ($word_limit + 1));
if(count($words) > $word_limit)
@FriendlyWP
FriendlyWP / page-template.php
Last active December 27, 2015 14:39
List custom post types organized by taxonomy. Show taxonomy titles. From http://wordpress.stackexchange.com/a/66232/16
$custom_terms = get_terms('custom_taxonomy');
foreach($custom_terms as $custom_term) {
wp_reset_query();
$args = array(
'post_type' => 'custom_post_type',
'tax_query' => array(
array(
'taxonomy' => 'custom_taxonomy',
'field' => 'slug',
@FriendlyWP
FriendlyWP / functions.php
Created November 5, 2013 17:42
Add file types to media library filters
/************** ADD FILE TYPES TO MEDIA LIBRARY FILTERS ****************/
add_filter( 'post_mime_types', 'custom_mime_types' );
function custom_mime_types( $post_mime_types ) {
$post_mime_types['application/msword'] = array( __( 'Word Docs' ), __( 'Manage Word Docs' ), _n_noop( 'Word Docs <span class="count">(%s)</span>', 'Word Docs <span class="count">(%s)</span>' ) );
$post_mime_types['application/vnd.ms-excel'] = array( __( 'Excel Files' ), __( 'Manage Excel Files' ), _n_noop( 'Excel Files <span class="count">(%s)</span>', 'Excel Files <span class="count">(%s)</span>' ) );
$post_mime_types['application/vnd.ms-powerpoint'] = array( __( 'PowerPoint Files' ), __( 'Manage PowerPoint Files' ), _n_noop( 'PowerPoint Files <span class="count">(%s)</span>', 'PowerPoint Files <span class="count">(%s)</span>' ) );
$post_mime_types['application/pdf'] = array( __( 'PDFs' ), __( 'Manage PDFs' ), _n_noop( 'PDFs <span class="count">(%s)</span>', 'PDFs <span class="count">(%s)</span>' ) );
@FriendlyWP
FriendlyWP / functions.php
Created November 1, 2013 21:46
Automatically add 'alt' tags to images if none exist. (Credit: https://gist.github.com/svil4ok/1991931)
// AUTOMATICALLY ADD ALT TAGS TO IMAGES
add_filter('the_content', 'add_alt_tags', 99999);
function add_alt_tags($content)
{
global $post;
preg_match_all('/<img (.*?)\/>/', $content, $images);
if(!is_null($images))
{
foreach($images[1] as $index => $value)
{
@FriendlyWP
FriendlyWP / functions.php
Created October 26, 2013 01:42
Remove dimensions from oEmbed videos so that YouTube and other oEmbedded videos resize to fit device width. This only works for videos that use <a href="http://codex.wordpress.org/Embeds">oEmbed</a>.
// VIDEO
// remove dimensions from oEmbed videos
add_filter( 'embed_oembed_html', 'tdd_oembed_filter', 10, 4 ) ;
function tdd_oembed_filter($html, $url, $attr, $post_ID) {
$return = '<figure class="video-container">'.$html.'</figure>';
return $return;
}
@FriendlyWP
FriendlyWP / functions.php
Created October 26, 2013 01:39
Display custom image sizes in Media uploader; insert custom image sizes into posts and pages
// Allows users to insert your custom image sizes via the Add Media button
// In this example, two custom image sizes are added to the defaults
add_filter( 'image_size_names_choose', 'my_custom_sizes' );
function my_custom_sizes( $sizes ) {
return array_merge( $sizes, array(
'thumb-narrow' => __('Narrow Thumbnail'),
'thumb-wide' => __('Wide Thumbnail'),
) );
}
@FriendlyWP
FriendlyWP / functions.php
Created October 26, 2013 01:32
Remove widget title if it begins with an exclamation point (!)
// REMOVE WIDGET TITLE IF IT BEGINS WITH EXCLAMATION POINT
// To use, add a widget and in the Title field put !The title here
// The title will show in the control panel, but not on the site itself
add_filter( 'widget_title', 'remove_widget_title' );
function remove_widget_title( $widget_title ) {
if ( substr ( $widget_title, 0, 1 ) == '!' )
return;
else
return ( $widget_title );
}
@FriendlyWP
FriendlyWP / functions.php
Created October 26, 2013 01:30
Enable shortcodes in all widgets
// ENABLE SHORTCODES IN ALL TEXT WIDGETS
add_filter('widget_text', 'do_shortcode');
@FriendlyWP
FriendlyWP / functions.php
Created October 26, 2013 01:29
Add custom default avatar
// add a new default avatar to the list in WordPress admin
// change path to reflect where your default image lives
add_filter( 'avatar_defaults', 'mytheme_addgravatar' );
function mytheme_addgravatar( $avatar_defaults ) {
$myavatar = get_bloginfo('stylesheet_directory') . '/_/images/avatar.gif';
$avatar_defaults[$myavatar] = 'New Default Gravatar';
return $avatar_defaults;
}
@FriendlyWP
FriendlyWP / functions.php
Last active March 22, 2016 23:26
Mailto email obfuscation shortcode. For anti-spam (spam-bots, spam fix, email mailto).
/* SHORTCODE FOR EMAIL OBFUSCATION
* usage: [email][email protected][/email]
* OR: [email address="[email protected]"]Contact Us[/email]
*/
add_shortcode('email', 'emailbot_ssc');
function emailbot_ssc($atts, $content = null) {
extract( shortcode_atts( array(
'address' => '',
), $atts ) );