Skip to content

Instantly share code, notes, and snippets.

View stellarcowboy's full-sized avatar

Kenneth White stellarcowboy

View GitHub Profile
@stellarcowboy
stellarcowboy / Facebook Conditional Load
Created February 11, 2012 16:59
Conditionally load Facebook social plugin based on screen width
<div id="fb-catcher"></div>
<script>
if (document.documentElement.clientWidth >= 600) {
document.getElementById('fb-catcher').innerHTML='<div id="fb-root"></div>';
}
(function(d, s, id) {
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) return;
js = d.createElement(s); js.id = id;
@stellarcowboy
stellarcowboy / gist:2363077
Created April 11, 2012 22:06
WordPress Image Attachment Toolbox
function get_string_between($string, $start, $end){ //required for parsing attachment URL's in attachment_image_retrieve
$string = " ".$string;
$ini = strpos($string,$start);
if ($ini == 0) return "";
$ini += strlen($start);
$len = strpos($string,$end,$ini) - $ini;
return substr($string,$ini,$len);
}
function attachment_image_retrieve($size = 'thumbnail', $limit = '-1') {
@stellarcowboy
stellarcowboy / gist:2910452
Created June 11, 2012 14:52
Display The Name Of The Custom Taxonomy For The Current Post - WordPress
$value = get_query_var($wp_query->query_vars['taxonomy']);
echo get_term_by('slug',$value,$wp_query->query_vars['taxonomy']);
@stellarcowboy
stellarcowboy / gist:2910459
Created June 11, 2012 14:53
Add Custom Post Type And Taxonomy Summary Count To Dashboard - WordPress
// Add all custom post types to the "Right Now" box on the Dashboard
add_action( 'right_now_content_table_end' , 'ucc_right_now_content_table_end' );
function ucc_right_now_content_table_end() {
$args = array(
'public' => true ,
'_builtin' => false
);
$output = 'object';
$operator = 'and';
@stellarcowboy
stellarcowboy / gist:2932639
Created June 14, 2012 20:13
Stop wordpress from wrapping images in <p> tags
// stop wordpress from wrapping images in <p> tags
function filter_ptags_on_images($content)
{
// do a regular expression replace...
// find all p tags that have just
// <p>maybe some white space<img all stuff up to /> then maybe whitespace </p>
// replace it with just the image tag...
return preg_replace('/<p>\s*(<a .*>)?\s*(<img .* \/>)\s*(<\/a>)?\s*<\/p>/iU', '\1\2\3', $content);
}
@stellarcowboy
stellarcowboy / gist:3130778
Created July 17, 2012 17:44
Correct the gallery URL attachemnt field WordPress
// Corrects the gallery URL attachment that disallows custom URLS
// Use get_post_meta( ATTACHMENT_ID_HERE, '_wp_attachment_url', true ) to retrieve
function _save_attachment_url($post, $attachment) {
if ( isset($attachment['url']) )
update_post_meta( $post['ID'], '_wp_attachment_url', esc_url_raw($attachment['url']) );
return $post;
}
add_filter('attachment_fields_to_save', '_save_attachment_url', 10, 2);
function _replace_attachment_url($form_fields, $post) {
@stellarcowboy
stellarcowboy / gist:4524909
Last active December 11, 2015 01:39
Given multiple rows of divs, equalize height of divs within each row.
var currentTallest = 0,
currentRowStart = 0,
rowDivs = new Array(),
$el,
topPosition = 0;
$('.blocks').each(function() {
$el = $(this);
topPostion = $el.position().top;
@stellarcowboy
stellarcowboy / index.php
Last active December 11, 2015 06:39
Test a database connection before installing WordPress
<?php
$server = "localhost";
$database = "database";
$username = "user";
$password = "password";
$mysqlConnection = mysql_connect($server, $username, $password);
if (!$mysqlConnection)
{
echo "Please try later.";
@stellarcowboy
stellarcowboy / gist:4574479
Last active December 11, 2015 08:38
WordPress: Fix pagination for static homepage with augmented query
// Fix pagination for static homepage with augmented query
function modify_query($query) {
if($query->is_main_query()) {
if (! get_query_var('paged')) {
$paged = (get_query_var('page')) ? get_query_var('page') : 1;
$query->set('paged',$paged);
}
}
}
add_action( 'pre_get_posts', 'modify_query' );
@stellarcowboy
stellarcowboy / gist:5240867
Last active December 15, 2015 09:49
WordPress: Pass post-meta image URL to processor for thumbnails
<?php $image_grabbed = substr_replace(get_post_meta($post->ID,{field_key},true), '-150x150', -4, 0); ?>
<?php echo $image_grabbed;?>