Skip to content

Instantly share code, notes, and snippets.

@ChrisLTD
ChrisLTD / parse_youtube_url.php
Created February 1, 2014 23:39
Put in a youtube URL of just about any format and get back the ID
<?php
function parse_youtube_url($url){
$id = '';
preg_match("/^.*(youtu.be\\/|v\\/|u\\/\\w\\/|embed\\/|watch\\?v=|\\&v=)([^#\\&\\?]*).*/uism", $url, $matches);
if( isset($matches[2]) ){
$id = $matches[2];
}
return $id;
}
@ChrisLTD
ChrisLTD / emailregex.js
Last active January 4, 2016 04:09
Regex for minimally testing valid email addresses. Looks for some characters, @ symbol, some characters, a period, then some more characters.
var regex = /^.+@.+\..+$/;
var tests = Array( '[email protected]', 'testATtest.com', 'test@testDOTcom',
'[email protected]', '[email protected]', '[email protected]');
for(var i = 0; i < tests.length; i++){
var result = regex.exec( tests[i] );
console.log('Testing: ' + tests[i]);
if( result ){
console.log('match found\n');
} else {
@ChrisLTD
ChrisLTD / admin.js
Created January 16, 2014 18:23
ACF Flexible Content layout collapse button
function close_flexible_content(){
$('.layout').attr('data-toggle', 'closed');
$('.acf-input-table').css('display', 'none');
}
function open_flexible_content(){
$('.layout').attr('data-toggle', 'open');
$('.acf-input-table').css('display', 'table');
}
close_flexible_content();
$('.field_type-flexible_content').prepend('<a class="toggle_flexible_content button button-small" style="float: right; margin-top: -8px;" href="#">Toggle Layout Boxes</a>')
@ChrisLTD
ChrisLTD / footer.php
Last active January 2, 2016 10:29
Rewrite <script> src links
<?php
$array = array(0, 1, 2, 3);
$string = print_r($array, true);
$filename = sha1( $string ) . ".sha1";
echo $filename . PHP_EOL;
@ChrisLTD
ChrisLTD / image_size_math.php
Last active December 19, 2015 23:08
Functions for calculating new proportional sizes for images
<?php
/*
* Get a new larger proportional height and width for an image
* Adapted from http://stackoverflow.com/a/4820929/648844
*
* Parameters: original width, original height, minimum width, minimum height
* Return: null if image exceeds minimums or array with rounded width and height
*/
function image_resize_up($orig_w, $orig_h, $MIN_W, $MIN_H){
$ratio = $orig_w * 1.0 / $orig_h;
@ChrisLTD
ChrisLTD / google-maps-example.html
Created July 14, 2013 00:27
Google Maps API V3 map w/ multiple markers, info boxes and auto center
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<title>Google Maps Multiple Markers</title>
<script src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.10.1.min.js"></script>
</head>
<body>
<div id="map" style="width: 500px; height: 400px;"></div>
@ChrisLTD
ChrisLTD / gist:5895564
Created June 30, 2013 15:23
Wordpress: Add custom taxonomy terms to body class
<?php
// Add custom taxonomy terms to body class
function section_taxonomy_in_body_class( $classes ){
if( is_singular() )
{
global $post;
$custom_terms = get_the_terms($post->ID, 'subsite');
if ($custom_terms) {
foreach ($custom_terms as $custom_term) {
$classes[] = 'section_' . $custom_term->slug;
@ChrisLTD
ChrisLTD / gist:5878178
Created June 27, 2013 16:55
Check if checkbox should be checked based on GET value
<?php
/*
* Check if checkbox should be checked based on GET value
*/
function isChecked($chkname,$value) {
if(!empty($_GET[$chkname])){
foreach($_GET[$chkname] as $chkval){
if($chkval == $value){
return true;
}
@ChrisLTD
ChrisLTD / gist:5877800
Created June 27, 2013 16:11
Loop through all custom taxonomy terms for a post type
<?php
$post_type = 'post';
// Get all the taxonomies for this post type
$taxonomies = get_object_taxonomies( (object) array( 'post_type' => $post_type ) );
foreach( $taxonomies as $taxonomy ){
// Gets every "category" (term) in this taxonomy to get the respective posts
$terms = get_terms( $taxonomy );
foreach( $terms as $term ){
echo $term->name;
}