Skip to content

Instantly share code, notes, and snippets.

View tcrsavage's full-sized avatar

Theo Savage tcrsavage

  • Wildebeest Webtech
  • Nottingham, United Kingdom
View GitHub Profile
@tcrsavage
tcrsavage / gist:5091330
Created March 5, 2013 15:59
parse csv file to php
function parse() {
if ( ! file_exists( $this->file_path ) || ! is_readable( $this->file_path ) )
return array();
$header = NULL;
$data = array();
if ( ( $handle = fopen( $this->file_path, 'r' ) ) !== FALSE ) {
@tcrsavage
tcrsavage / image-replacer.php
Created July 16, 2013 02:49
Replace all broken attachment images with a default image - great for doing local dev with an imported database
add_filter( 'wp_get_attachment_image_attributes', function( $attr, $attachment ) {
$content_path = ( defined( 'IMAGE_REPLACER_WP_CONTENT_PATH' ) ) ? IMAGE_REPLACER_WP_CONTENT_PATH : WP_CONTENT_DIR;
$content_url = ( defined( 'IMAGE_REPLACER_WP_CONTENT_URL' ) ) ? IMAGE_REPLACER_WP_CONTENT_URL : WP_CONTENT_URL;
$file_path = str_replace( $content_url, $content_path, $attr['src'] );
if ( file_exists( $file_path ) )
return $attr;
@tcrsavage
tcrsavage / gist:6017245
Created July 17, 2013 02:36
Default the_post_thumbnail() and get_the_post_thumbnail() to return the first image in post content if no featured image is set
//If no post thumbnail exists, get the first image in the post and return that instead
add_filter( 'post_thumbnail_html', function( $html, $post_id, $post_thumbnail_id, $size, $attr ) {
if ( $html )
return $html;
$found = preg_match( '/<img.+?class=\".+?([0-9]+).*\/>/', get_post( $post_id )->post_content, $matches );
if ( $found )
$html = wp_get_attachment_image( end( $matches ), $size, false, $attr );
@tcrsavage
tcrsavage / gist:6154325
Created August 5, 2013 08:37
Replace broken images in post content with a default image - Useful for dev environments
add_filter( 'the_content', function( $content ) {
$content_path = ( defined( 'IMAGE_REPLACER_WP_CONTENT_PATH' ) ) ? IMAGE_REPLACER_WP_CONTENT_PATH : WP_CONTENT_DIR;
$content_url = ( defined( 'IMAGE_REPLACER_WP_CONTENT_URL' ) ) ? IMAGE_REPLACER_WP_CONTENT_URL : WP_CONTENT_URL;
preg_match_all( '/<img(?:\s|.)+?src="((?:\s|.)+?)"(?:\s|.)+?\/?>/', $content, $matches );
if ( empty( $matches[1] ) )
return $content;
@tcrsavage
tcrsavage / gist:e61fa2cf1a458671b4bc
Last active August 29, 2015 14:14
WorddPress Self unhooking nested anonymous function
//Ensure that we don't allow creation of tickers via insertion of a post with non-existent tickers assigned
//Tickers should only be created via the admin ticker interface or via automatic pull-in from xignite
add_filter( 'wp_insert_post_data', function( $data, $post_arr ) {
if ( empty( $post_arr['tax_input']['ticker'] ) ) {
return $data;
}
$created_tickers = array();
@tcrsavage
tcrsavage / main.dart
Created June 3, 2020 12:51
Dart construct API request URL with default GET params
main() {
Map <String, String> args = {'filter_visibility' : 'false' };
print( getMediaAssetCollectionArgs( args ) );
print( getRequestURi( getMediaAssetCollectionArgs( args ) ) );
}
Map getMediaAssetCollectionArgs( Map args ) {