Skip to content

Instantly share code, notes, and snippets.

View michaelstoffer's full-sized avatar
:octocat:

Michael Stoffer michaelstoffer

:octocat:
View GitHub Profile
<?
if(isset($_GET['image-sizes'])) {
add_action('wp', function() {
require_once(ABSPATH.'wp-admin/includes/media.php');
require_once(ABSPATH.'wp-admin/includes/file.php');
require_once(ABSPATH.'wp-admin/includes/image.php');
// Get image sizes defined by theme/plugins and sort them by width DESC
function get_ordered_image_sizes() {
$image_sizes = wp_get_additional_image_sizes();
<?
/**
* Programmatically sideload an image from a URL and set all attachment data and meta
* @param string $url URL of image to sideload
* @param array $data['filename'] Image filename
* @param array $data['alt'] Image alt text
* @param array $data['description'] Image description
* @param array $data['caption'] Image caption
* @param string $post_id Post ID to attach image to
* @param boolean $generate_meta Generate attachment metadata?
<?
/**
* Builds a responsive HTML <img> tag based on the image sizes passed or defined by active themes and plugins
*
* @param array $attributes Associative array of attributes for the <img> tag
*
* @return string HTML <img> tag
*/
function mdg_img($attributes = []) {
// Defaults
<?
/**
* Builds a responsive HTML <img> tag based on the image sizes passed or defined by active themes and plugins
*
* @param int|array $attachment_id Attachment ID
* @param array $image_sizes Associative array of image size names defined by active themes and plugins
* @param array $attributes Associative array of attributes for the <img> tag
*
* @return string HTML <img> tag
*/
add_filter('post_type_link', function($permalink, $post) {
if ($post->post_type == 'doctorlandings') {
$permalink = get_post_meta($post->ID, 'doctorlandings_office_website', true);
}
return $permalink;
}, 10, 2);
@michaelstoffer
michaelstoffer / chunking_cached_object.php
Last active April 16, 2021 20:01
This is code that allows us to create multiple chunks for the cached object
<?
public function flushCache() {
$iterations = get_option('__website_cache_metadata_boats_count');
for ($loops = 0; $loops <= ($iterations - 1); $loops++) {
delete_option('__website_cache_metadata_boats_'.$loops);
}
delete_option('__website_cache_metadata_boats_count');
}
public function loadData() {
// Convert currency from one type to another
private function convertCurrency($val, $base = 'EUR', $to = 'USD') {
$json = file_get_contents('https://api.exchangeratesapi.io/latest?base='.$base);
if(!empty($json)) $json = json_decode($json);
return (float)preg_replace('/,/', '', $val) * $json->rates->{$to};
}
@michaelstoffer
michaelstoffer / determineCurrency.php
Last active April 16, 2021 20:02
Get currency data based on string passed
<?
// Get currency data based on string passed
private function determineCurrency($cur_string) {
$currency = [];
switch(true) {
case substr($cur_string, 0, 7) == '&#8364;':
$currency['type'] = 'EUR';
$currency['val'] = substr($cur_string, 7);
break;
case substr($cur_string, 0, 5) == '&#36;':
@michaelstoffer
michaelstoffer / dabblet.css
Created March 31, 2012 18:31
The first commented line is your dabblet’s title
/**
* The first commented line is your dabblet’s title
*/
background: #f06;
background: linear-gradient(45deg, #f06, yellow);
min-height: 100%;