Skip to content

Instantly share code, notes, and snippets.

View trovster's full-sized avatar

Trevor Morris trovster

View GitHub Profile
@trovster
trovster / gist:1541636
Created December 30, 2011 21:51
Custom_Do_Action WordPress class for shortcode hook of functions and do_actions
<?php
class Custom_Do_Action {
/**
* __construct()
* @param string
* @param array
*/
public function __construct($options = array()) {
foreach($options as $key => $value) {
@trovster
trovster / gist:2252907
Created March 30, 2012 16:57
A PHP function to output start and end dates, allowing for for short syntax March 3rd-10th 2012 (if in same month & year).
<?php
/**
* event_output_start_end_date
* @param string $start_date
* @param string $end_date
* @return string
*/
function event_output_start_end_date($start_date, $end_date) {
$start_datetime = strtotime($start_date); // yyyy-mm-dd
$end_datetime = strtotime($end_date); // yyyy-mm-dd
@trovster
trovster / functions-general.php
Created April 19, 2012 10:18
My general checkbox meta box for the WordPress admin area. All custom fields prefix with custom_ are saved automatically with my update function.
<?php
function post_type_custom_fields_general_featured() {
global $post;
$custom = get_post_custom($post->ID);
$is_featured = is_custom_boolean($custom, 'custom_is_featured');
echo '<p class="checkbox" style="padding-top: 5px;">';
echo '<input type="hidden" name="custom_is_featured" value="false" />';
echo '<input style="margin-right: 5px;" id="custom_is_featured" value="true" type="checkbox" name="custom_is_featured"' . (($is_featured) ? ' checked="checked"' : '') . ' />';
@trovster
trovster / gist:2420303
Created April 19, 2012 11:09
Simple custom get custom field function, which handles when a key doesn't exist.
<?php
function template_get_custom_field($custom, $key, $prefix = 'custom_') {
if(!empty($custom[$prefix . $key][0])) {
return $custom[$prefix . $key][0];
}
return '';
}
// examples
$custom = get_post_custom($post->ID);
@trovster
trovster / functions.php
Created July 13, 2012 11:35
Latest Posts
<?php
/**
* template_pre_loop
* @desc Used before a custom loop
* Parameter is the WP_Query options for the new loop
* Saves the original query and post data
* Returns the new query, along with the original wp_query and post
* @param array $options
* @return array
*/
@trovster
trovster / gist:4319969
Created December 17, 2012 17:13 — forked from anonymous/gist:4319896
If you have Tweenest —http://pongsocket.com/tweetnest/ — installed, then the following two functions will pull that data in to your WordPress install. This code assumes that the default table prefix is used (tn_).
<?php
function get_tweetnest_tweets($args) {
global $wpdb;
$args = shortcode_atts(array(
'count' => 5,
), $args);
$sql = sprintf("SELECT `tn_tweets`.*, `tn_tweetusers`.`screenname`, `tn_tweetusers`.`realname`, `tn_tweetusers`.`profileimage`
FROM `tn_tweets`
@trovster
trovster / settings.php
Created March 21, 2013 15:39
I was bored of waiting for clients to give me their social network URLs, so I’ve written a very basic settings class which adds social network options to the CMS. The script adds the input fields to the “General” settings page, as well as adding a new page under “Settings”
<?php
/*
Component: Settings
Description: WordPress setting options
Author: Surface / Trevor Morris
Author URI: http://www.madebysurface.co.uk
Version: 0.0.1
*/
class Surface_Settings {
@trovster
trovster / twitter.js
Created June 10, 2013 15:51
Append your CSS to the Twitter Timeline widget to allow for more styling options. In your normal CSS file(s) you can then use .twitter-timeline .stream {}
$('.twitter-timeline').parent().on('DOMNodeInserted', function (event) {
$(event.target).closest('iframe').on('load', function (e) {
$(e.target).contents().find('head').append($('link[rel="stylesheet"]').clone());
});
});
@trovster
trovster / functions.php
Created July 4, 2013 18:12
For some reason hosting company ‘names.co.uk’ was setting a permission of 600 for WordPress uploaded files and subsequently resized images. Not very useful. These two hooks should fix this issue. #WordPress
<?php
/**
* _site_wp_handle_upload_fix_permissions
* @desc Set the correct permission for the uploaded file.
* @param array $file
* @param string $type
*/
function _site_wp_handle_upload_fix_permissions($file, $type = 'upload') {
chmod($file['file'], 0644);
@trovster
trovster / functions.php
Last active December 25, 2015 16:59
Make the default comment_form more Gravity Form-like, with class names and wrappers.
<?php
/**
* _site_comment_form_top
* @desc Wrap the comment form fields (not submit) in a <div> (open)
*/
function _site_comment_form_top() {
echo '<div class="gform_body">';
}
add_action('comment_form_top', '_site_comment_form_top');