Skip to content

Instantly share code, notes, and snippets.

View codearachnid's full-sized avatar
🎯
Focusing

Timothy Wood codearachnid

🎯
Focusing
View GitHub Profile
@codearachnid
codearachnid / getEventCounts.php
Created December 14, 2012 17:47
A quick and dirty getEventCounts - not working yet
<?php
public static function getEventCounts( $args = array() ){
global $wpdb;
$date = date( 'Y-m-d' );
$defaults = array(
'post_type' => TribeEvents::POSTTYPE,
'start_date' => tribe_event_beginning_of_day( $date ),
'end_date' => tribe_event_end_of_day( $date ),
@codearachnid
codearachnid / sprintf_assoc.php
Last active December 10, 2015 16:39
vsprintf, sprintf, and printf do not allow for associative arrays to perform replacements `sprintf_assoc` resolves this by using the key of the array in the lookup for string replacement. http://php.net/manual/en/function.vsprintf.php
<?php
function sprintf_assoc( $string = '', $replacement_vars = array(), $prefix_character = '%' ) {
if ( ! $string ) return '';
if ( is_array( $replacement_vars ) && count( $replacement_vars ) > 0 ) {
foreach ( $replacement_vars as $key => $value ) {
$string = str_replace( $prefix_character . $key, $value, $string );
}
}
return $string;
@codearachnid
codearachnid / custom_image_size_names_choose.php
Created January 7, 2013 13:43
Append new image sizes to the media upload/insert interface for WordPress.
<?php
add_filter( 'image_size_names_choose', 'custom_image_size_names_choose' );
function custom_image_size_names_choose( $sizes ) {
global $_wp_additional_image_sizes;
// exit gracefully if there are no custom sizes
if ( empty($_wp_additional_image_sizes) )
return $sizes;
@codearachnid
codearachnid / array_splice_key.php
Last active September 20, 2016 23:58
PHP array management has some limits to merging associative arrays. These methods allow for creative positional merging of associative arrays by key. Think of this as array_splice for associative arrays. Another clever option I found recently is http://stackoverflow.com/a/13289272/1542064
<?php
/**
* Insert another array into an associative array after the supplied key
*
* @param string $key
* The key of the array you want to pivot around
* @param array $source_array
* The 'original' source array
* @param array $insert_array
@codearachnid
codearachnid / attr_unescape.js
Created January 20, 2013 14:04
When using jQuery to update an attribute of a selector and use special characters like ampersand (&) in the value it will escape the characters when using `.attr( key, value )`. Technically it isn't jQuery escaping anything since escaping is only necessary in a context where the character would otherwise have a special meaning in the eyes of the…
/**
* Escape the .attr() method in jQuery
*
* Creates an empty element using jQuery, set it's HTML to the text you want then return the value as "text"
*
* @param string
* @return string
*/
function attr_unescape( x ){
return jQuery('<div />').html( x ).text();
@codearachnid
codearachnid / truncate.css
Last active December 11, 2015 10:48
Needed a clever solution to truncate text within variable width containers after looking through several jQuery solutions I settled on a CSS3 cross browser option.
.truncate {
/* add max-width if you wish to make the cutoff text consistent vs variable width of container */
display:inline-block;
overflow:hidden;
text-overflow: ellipsis; /* limited browser support for anything but clip/ellipsis */
white-space:nowrap;
}
@codearachnid
codearachnid / wp_convert_to_timezone.php
Last active September 19, 2023 17:21
Recently was working on importing dates into WordPress and needed to convert the date to the current site timezone. Because WordPress may not give you the timezone string if an UTC offset is set I modify the datetime vs change the timezone.
<?php
/**
* wp_convert_to_timezone useful for adjusting an imported DateTime to your site GMT/UTC offset
*
* @link http://www.php.net/manual/en/timezones.php Accepted $timezone strings
* @link http://www.cs.tut.fi/~jkorpela/iso8601.html Reason for default return as ISO 8601
*
* @param string $datetime
* @param string $timezone default GMT
@codearachnid
codearachnid / tribe_patch_convert_to_timezone.php
Last active December 12, 2015 03:09
Fix for Facebook Event API "bug" that forces all events to use PAC time. http://tri.be/support/forums/topic/maybe-usefull-code-for-timezones-fix/ * Make sure you remove this once you're using any version of Tribe's Facebook Events greater than version 1.0.4
<?php
// fix for http://tri.be/support/forums/topic/maybe-usefull-code-for-timezones-fix/
// remove this once you're using any version of Tribe's Facebook Events greater than version 1.0.4
add_filter( 'tribe_fb_parse_facebook_event', 'tribe_patch_facebook_timezone');
function tribe_patch_facebook_timezone( $event_params ){
// build date string
$start_date = $event_params['EventStartDate'] . ' ' . str_pad($event_params['EventStartHour'], 2, '0', STR_PAD_LEFT) . ':' . str_pad($event_params['EventStartMinute'], 2, '0', STR_PAD_LEFT) . ' ' . $event_params['EventStartMeridian'];
@codearachnid
codearachnid / debug_registered_associations.php
Last active December 12, 2015 05:48
I wanted an easy way to show what post type & taxonomy objects had associative registrations. This will spit out a simple table listing the post type -> taxonomy relationships
<?php
function debug_registered_associations(){
global $wp_post_types;
global $wp_taxonomies;
$header = '<tr><th colspan="2">%s</th></tr><tr><th>Post Type</th><th>Taxonomy</th></tr>';
?>
<style>
.debug_registered_associations{border:1px solid #eee;margin:10px;}
.debug_registered_associations th{text-align: left;}
@codearachnid
codearachnid / reorder_category_posts.php
Last active December 14, 2015 01:28
Reworking the sticky post reordering
<?php
/**
* Places the sticky post at the top of the list of posts for the category that is being displayed.
*
* @param $posts The lists of posts to be displayed for the given category
* @return The updated list of posts with the sticky post set as the first titem
*/
function reorder_category_posts( $posts ) {