Skip to content

Instantly share code, notes, and snippets.

@oranblackwell
Last active August 29, 2015 14:01
Show Gist options
  • Save oranblackwell/08be6f91ac9c88a990bc to your computer and use it in GitHub Desktop.
Save oranblackwell/08be6f91ac9c88a990bc to your computer and use it in GitHub Desktop.

Various Wordpress Snippets

Add image dimensions

add_image_size( 'category-single', 285, 220, true ); // home page grid & category side
add_image_size( 'category-double', 285, 510, true ); // home page grid

Show which template file is currently being used.

/**
 * Shows which template file is currently being used.
 */
add_action( 'wp_head', 'show_template' );
function show_template() {
	global $template;
	debug( $template ); // || print_r($template)
}

Add JavaScript to footer

/**
 * Add script to footer.
 */
add_action( 'wp_footer', 'footer_js' );
function footer_js() {
	?>
	<script type="text/javascript">
		if (undefined !== window.jQuery) {
			jQuery('.carousel').carousel()
		}
	</script>
<?php
}

Add Thumbnails in Manage Posts/Pages List

/**
 * Add Thumbnails in Manage Posts/Pages List
 */
if ( ! function_exists( 'AddThumbColumn' ) && function_exists( 'add_theme_support' ) ) {

	// for post and page
	add_theme_support( 'post-thumbnails', array( 'post', 'page' ) );

	function AddThumbColumn( $cols ) {
		$cols['thumbnail'] = __( 'Thumbnail' );
		return $cols;
	}

	function AddThumbValue( $column_name, $post_id ) {
		$width  = (int) 35;
		$height = (int) 35;

		if ( 'thumbnail' == $column_name ) {
			// thumbnail of WP 2.9
			$thumbnail_id = get_post_meta( $post_id, '_thumbnail_id', true );
			// image from gallery
			$attachments = get_children( array( 'post_parent' => $post_id, 'post_type' => 'attachment', 'post_mime_type' => 'image' ) );
			if ( $thumbnail_id ) {
				$thumb = wp_get_attachment_image( $thumbnail_id, array( $width, $height ), true );
			} elseif ( $attachments ) {
				foreach ( $attachments as $attachment_id => $attachment ) {
					$thumb = wp_get_attachment_image( $attachment_id, array( $width, $height ), true );
				}
			}
			if ( isset( $thumb ) && $thumb ) {
				echo $thumb;
			} else {
				echo __( 'None' );
			}
		}
	}

	// for posts
	add_filter( 'manage_posts_columns', 'AddThumbColumn' );
	add_action( 'manage_posts_custom_column', 'AddThumbValue', 10, 2 );

	// for pages
	add_filter( 'manage_pages_columns', 'AddThumbColumn' );
	add_action( 'manage_pages_custom_column', 'AddThumbValue', 10, 2 );
}

Tests if any of a post's assigned categories are descendants of target categories

/**
 * Tests if any of a post's assigned categories are descendants of target categories
 *
 * @link http://codex.wordpress.org/Function_Reference/in_category#Testing_if_a_post_is_in_a_descendant_category
 */
if ( ! function_exists( 'post_is_in_descendant_category' ) ) {
	function post_is_in_descendant_category( $cats, $_post = null ) {
		foreach ( (array) $cats as $cat ) {
			// get_term_children() accepts integer ID only
			$descendants = get_term_children( (int) $cat, 'category' );
			if ( $descendants && in_category( $descendants, $_post ) ) {
				return true;
			}
		}

		return false;
	}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment