Skip to content

Instantly share code, notes, and snippets.

@tjhole
tjhole / WORDPRESS: Base64 Image to Wordpress Uploads directory
Created May 9, 2014 12:35
WORDPRESS: Base64 Image to Wordpress Uploads directory
function tattoo_submit() {
if (isset($_POST["addtattoo"])) {
$title = "Tattoo : ". $_POST["tatooInput"];
$my_post = array(
'post_title' => $title,
'post_status' => 'publish',
'post_author' => 1,
@tjhole
tjhole / WORDPRESS: CDN Function
Created May 9, 2014 10:26
WORDPRESS: CDN Function
function my_cdn_upload_url() {
return 'http://static.mahtabhussain.com/content/uploads';
}
add_filter( 'pre_option_upload_url_path', 'my_cdn_upload_url' );
@tjhole
tjhole / WORDPRESS: Debug On
Created May 6, 2014 10:05
WORDPRESS: Debug On
define('WP_DEBUG', true);
define('WP_DEBUG_LOG', true);
define('WP_DEBUG_DISPLAY', false);
@ini_set('display_errors',0);
ini_set('log_errors',TRUE);
ini_set('error_reporting', E_ALL);
ini_set('error_log', dirname(__FILE__) . '/error_log.txt');
@tjhole
tjhole / WORDPRESS: Save GPS Meta on Upload
Created April 24, 2014 16:13
WORDPRESS: Save GPS Meta on Upload
function add_geo_exif($meta,$file,$sourceImageType) {
$exif = @exif_read_data( $file );
if (!empty($exif['GPSLatitude']))
$meta['latitude'] = $exif['GPSLatitude'] ;
if (!empty($exif['GPSLatitudeRef']))
$meta['latitude_ref'] = trim( $exif['GPSLatitudeRef'] );
if (!empty($exif['GPSLongitude']))
$meta['longitude'] = $exif['GPSLongitude'] ;
if (!empty($exif['GPSLongitudeRef']))
$meta['longitude_ref'] = trim( $exif['GPSLongitudeRef'] );
@tjhole
tjhole / WORDPRESS: Create safe ID from ACF title
Created April 19, 2014 13:15
WORDPRESS: Create safe ID from ACF title
function safeID( $string, $separator = '_' )
{
$accents_regex = '~&([a-z]{1,2})(?:acute|cedil|circ|grave|lig|orn|ring|slash|th|tilde|uml);~i';
$special_cases = array( '&' => 'and');
$string = mb_strtolower( trim( $string ), 'UTF-8' );
$string = str_replace( array_keys($special_cases), array_values( $special_cases), $string );
$string = preg_replace( $accents_regex, '$1', htmlentities( $string, ENT_QUOTES, 'UTF-8' ) );
$string = preg_replace("/[^a-z0-9]/u", "$separator", $string);
$string = preg_replace("/[$separator]+/u", "$separator", $string);
@tjhole
tjhole / PHP: String to ID
Created April 9, 2014 11:41
PHP: String to ID
function toID($string) {
//Lower case everything
$string = strtolower($string);
//Make alphanumeric (removes all other characters)
$string = preg_replace("/[^a-z0-9_\s-]/", "", $string);
//Clean up multiple dashes or whitespaces
$string = preg_replace("/[\s-]+/", " ", $string);
//Convert whitespaces and underscore to dash
$string = preg_replace("/[\s_]/", "-", $string);
return $string;
@tjhole
tjhole / WORDPRESS: Redirect based on location using Cloudflare
Created April 4, 2014 10:27
WORDPRESS: Redirect based on location using Cloudflare
<?php
// Geo Redirect using Cloudflare
$activepage = $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
$country_code = $_SERVER["HTTP_CF_IPCOUNTRY"];
$page_from = "www.domain.tld/";
$page_to = "www.newdomain.tld/";
?>
<!-- Country Code : <?php echo $country_code; ?> -->
<!-- Active URL : <?php echo $activepage; ?> -->
@tjhole
tjhole / WORDPRESS: rename admin items
Created April 3, 2014 11:26
WORDPRESS: Rename admin items
add_filter('gettext', 'rename_admin_menu_items');
add_filter('ngettext', 'rename_admin_menu_items');
/**
* Replaces wp-admin menu item names
*
* @author Daan Kortenbach
*
* @param array $menu The menu array.
*
* @return $menu Menu array with replaced items.
@tjhole
tjhole / ACF: Image (Interchange Foundation 5)
Created April 2, 2014 14:56
ACF: Image (Interchange Foundation 5)
<?php
$attachment_id = get_field('field_name');
$image_thumbnail = wp_get_attachment_image_src( $attachment_id, "thumbnail" );
$image_medium = wp_get_attachment_image_src( $attachment_id, "medium" );
$image_large = wp_get_attachment_image_src( $attachment_id, "large" );
// url = $image[0];
// width = $image[1];
// height = $image[2];
?>
<img data-interchange="[<?php echo $image_thumbnail[0]; ?>, (default)], [<?php echo $image_medium[0]; ?>, (large)]"/>
@tjhole
tjhole / WORDPRESS: Menu Short
Created February 21, 2014 16:22
WORDPRESS: Menu Short
<?php
$defaults = array(
'theme_location' => 'primary',
'container' => 'false',
'menu_class' => 'menu',
'menu_id' => 'nav',
'items_wrap' => '<ul id="%1$s" class="%2$s">%3$s</ul>',
);