Skip to content

Instantly share code, notes, and snippets.

View SleeplessByte's full-sized avatar
💎
💎 💎 💎

Derk-Jan Karrenbeld SleeplessByte

💎
💎 💎 💎
View GitHub Profile
@SleeplessByte
SleeplessByte / helper_get_current_url.php
Last active December 10, 2015 22:18
Gets the current displayed url for a page. key <> value pairs can be stripped from the url by key.
<?php
/**
* Gets the current displayed url
* @strip array of key names to be stripped from the url
* @returns the url
*/
protected function get_current_url( $strip = NULL ) {
$url = isset( $_SERVER["HTTPS"] ) && $_SERVER['HTTPS'] == "on" ? 'https://' : 'http://';
$url .= ( strlen( $_SERVER["SERVER_NAME"] ) ? $_SERVER["SERVER_NAME"] : $_SERVER['HTTP_HOST']);
$url .= ( !in_array( strval( $_SERVER["SERVER_PORT"] ), array( "80", "443" ) ) ) ? $_SERVER["SERVER_PORT"] : '';
@SleeplessByte
SleeplessByte / wp_thumbnail_base_size.php
Last active December 10, 2015 22:18
Missing function to get the defined thumbnail size metadata and a check to see if an attachment's thumbnail is actually of that defined size.
<?php
/**
* Gets the thumbnail resize sizes.
*
* These are the sizes defined by the media options page or custom added sizes, rather than the actual
* thumbnail image sizes. Falls back to thumbnail if size does not exist. Put your size parameter first
* through the 'post_thumbnail_size' filter if you want WordPress get_post_thumbnail behaviour.
*
* @size either string with thumbnail name size or array with dimensions
* @returns array with width, height and crop parameters
@SleeplessByte
SleeplessByte / wp_plugin_i18n.php
Last active December 10, 2015 22:28
Loads the textdomain for a plugin.
<?php
/**
* Preferably the plugin is (in) a class, so use:
* add_action( array( &$this, 'textdomain_init');
*/
add_action( 'plugins_loaded', 'textdomain_init' );
/**
* Initializes the textdomain for this plugin
*/
@SleeplessByte
SleeplessByte / wp_custom_login.php
Last active December 10, 2015 22:38
Changes the login logo to a theme based one.
<?php
/**
* Changes the login logo and sets the appropiate sizes
*/
function custom_login_logo() {
$path = '/images/logo_login.png';
$size = getimagesize( get_template_directory() . $path );
if ( $size ) :
echo '<style type="text/css">
.login h1 a {
@SleeplessByte
SleeplessByte / helper_array_to_listing.php
Last active May 16, 2019 09:25
Creates a human readable list of an array, implodes all items, but combines last two first.
<?php
/**
* Creates a human readable list of an array
*
* @param string[] $ranges array to list items of
* @param string $glue normal glue between items
* @param string $last glue between last two items
*
* @remarks works with 0, 1, 2 or 3+ items
* @returns string 'item1, item2, item3 or item4'
@SleeplessByte
SleeplessByte / css_ie7_hacks.css
Created January 15, 2013 21:19
CSS Hacks for Internet Explorer 7. Currently has: inline-block-hack
.inline-block-hack {
/* Other css */
zoom:1; /* << sets has layout */
*display:inline; /* << but inline (with layout = inline-block) */
}
@SleeplessByte
SleeplessByte / regex_nested.php
Last active December 11, 2015 23:08
Matches nested tags such as [foo attr="data" loose ][foo]inner content[bar invisible="true"/][/foo][bar]lalala[/bar][/foo][foo]love[/foo] correctly. Simply recall on element->inner_content for recursive processing.
<?php
function regex_nested( $content ) {
$pattern = "/\[(?P<type>[^\]\s]+)(?P<props>[^\]]*?)((?P<tagclose>\s*\/\])|".
"(\](?P<inner>([^\[]*|\<\!\-\-.*?\-\-\>|(?R))*)\[\/\\1\s*\]))/sm";
if ( !preg_match_all($pattern, $content, $matches, PREG_OFFSET_CAPTURE) )
return $content;
$elements = array();
foreach ( $matches[0] as $key => $match ) {
array_push( $elements, (object)array(
@SleeplessByte
SleeplessByte / regex_attributes.php
Created January 30, 2013 16:22
Reads out attributes foo="bar" octocat as two elements (foo => bar and octocat => )
<?php
function regex_attributes( &$element ) {
$matches = array();
$pattern = '/\s*(?P<key>[^\s=\'"]+)(?:=(?:([\'"])(?P<value>[^\'"]+)[\'"])|\s|$)\s*/sm';
if ( !preg_match_all( $pattern, $element->attributes_raw, $matches, PREG_OFFSET_CAPTURE ) )
return;
foreach ( $matches[0] as $key => $match )
$element->attributes[ $matches['key'][$key][0] ] = $matches['value'][$key][0];
}
@SleeplessByte
SleeplessByte / do_prepared_query.php
Created April 30, 2013 18:20
Executes a prepared query. Provide with a query. Use ? in the query for bind locations. Types is a string where its length equals the values array size. Returns the results in an associative array, insert id, NULL or success boolean.
<?php
/**
* Executes a prepared query
*
* @param string $query the query to execute
* @param string $types the types of the bind (s: string or d: integer for each value)
* @param mixed[] $values array of values
* @param string $error referenced error variable
* @returns null|boolean|integer|mixed[] the results
*/
@SleeplessByte
SleeplessByte / resize_chart_canvas.js
Created May 26, 2013 19:46
Canvas resizer for chart with debouncing, so chart or canvas doesn't crash.
$(document).ready( function(){
// Thank you underscorejs
var debounce = function(func, wait, immediate) {
var timeout, result;
return function() {
var context = this, args = arguments;
var later = function() {
timeout = null;
if (!immediate) result = func.apply(context, args);