Skip to content

Instantly share code, notes, and snippets.

@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 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 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 / 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 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 / functions.php
Created November 22, 2013 00:55
Remove menu items (hide menu items) for everyone except a specific user.
/*
* See http://wordpress.stackexchange.com/a/28784/16 for all options
*/
add_action( 'admin_menu', 'my_remove_menu_pages', 9999 );
function my_remove_menu_pages() {
global $current_user;
$username = $current_user->user_login;
if ($username !== 'SPECIFIC-ADMIN-USERNAME-HERE') {
remove_menu_page('edit-comments.php');
@FriendlyWP
FriendlyWP / functions.php
Created November 22, 2013 01:04
Hide the master administrator-level account so it can't accidentally be deleted via the admin panel.
// HIDE MASTER ADMIN ACCOUNT SO IT CAN'T BE ACCIDENTALLY DELETED
add_action('pre_user_query','yoursite_pre_user_query');
function yoursite_pre_user_query($user_search) {
global $current_user;
$username = $current_user->user_login;
if ($username !== 'MASTER_ADMIN_USERNAME_HERE') {
global $wpdb;
$user_search->query_where = str_replace('WHERE 1=1',
"WHERE 1=1 AND {$wpdb->users}.user_login != 'MASTER_ADMIN_USERNAME_HERE'",$user_search->query_where);
@FriendlyWP
FriendlyWP / functions.php
Created November 22, 2013 18:37
Fixes page navigation giving 404 errors on page/2 and beyond when using custom permalinks like category/postname.
// IF USING CUSTOM PERMALINKS LIKE CATEGORY/POSTNAME, PAGE NAVIGATION BREAKS ON PAGE 2. THIS IS THE FIX
// from http://barefootdevelopment.blogspot.com/2007/11/fix-for-wordpress-paging-problem.html
add_filter('request', 'remove_page_from_query_string');
function remove_page_from_query_string($query_string) {
if ($query_string['name'] == 'page' && isset($query_string['page'])) {
unset($query_string['name']);
// 'page' in the query_string looks like '/2', so split it out
list($delim, $page_index) = split('/', $query_string['page']);
$query_string['paged'] = $page_index;
}
@FriendlyWP
FriendlyWP / functions.php
Created November 22, 2013 23:45
Add simple section navigation (child/parent hierarchy) via a shortcode.
// DISPLAY SIMPLE SECTION NAVIGATION VIA SHORTCODE
// adapted from the fantastic Simple Section Navigation plugin which is alas no longer being maintained
// http://wordpress.org/plugins/simple-section-navigation/
add_shortcode('simplesidenav', 'mm_simplesidenav');
function mm_simplesidenav( $atts, $content = null) {
extract( shortcode_atts( array(
'exclude' => '',
'topic' => '',
//'hierarchical' => 'false',
), $atts ) );
@FriendlyWP
FriendlyWP / flexslider-youtube.js
Created January 11, 2014 19:26
Trying to get Flexslider to work with YouTube videos playing and pausing the slideshow. Here's a live example of the below code: http://tmi.friendlyweb.us/people/sample-page/
jQuery(document).ready(function($) {
$.getScript('//www.youtube.com/iframe_api');
var cnt = 0;
$('.flexslider iframe[src*=youtube]').each(function() {
$(this).attr('id', 'youtubeplayer' + cnt);
cnt++;
});
loadSlider();