Skip to content

Instantly share code, notes, and snippets.

View bcole808's full-sized avatar

Ben Cole bcole808

View GitHub Profile
@bcole808
bcole808 / get_random_attachment_id_from_post.php
Created May 10, 2017 21:15
Get WordPress attachment from post
<?php
function get_random_attachment_id_from_post($post = 0) {
$post = get_post( $post );
if ( ! preg_match_all( '/<img [^>]+>/', $post->post_content, $matches ) ) {
return 0;
}
$attachment_ids = array();
@bcole808
bcole808 / slrlounge_env_redirect.js
Created September 30, 2016 16:39
Environment Redirect Bookmarklet
(function(){
var is_slrlounge_url = /https?:\/\/(www.|staging.)?slrlounge\.(dev|com)/;
if (null === is_slrlounge_url.exec(window.location)) {
alert("YES!");
} else {
alert("NO.");
}
@bcole808
bcole808 / class-remove-image-sizes.php
Last active October 14, 2015 16:59
WP Optimize Images Resizing
<?php
// Exit if accessed directly
! defined( 'ABSPATH' ) && exit;
// Check if class already exists
if ( ! class_exists( 'OIR_Remove_Image_Sizes' ) ) :
final class OIR_Remove_Image_Sizes {
@bcole808
bcole808 / ui-scaffolding.scss
Created August 31, 2015 17:50
SCSS mixin for UI-Scaffolding
@mixin ui-scaffolding($color:"rgba(0,255,100)", $label:"PLACEHOLDER") {
position: relative;
outline: 1px solid $color;
background: rgba($color, 0.2);
&:before {
content: $label;
display: block;
position: absolute;
top: 50%;
@bcole808
bcole808 / wp-router.php
Created June 2, 2015 20:54
WordPress PHP Router
<?php
######### router.php #########
#
# This file implements basic server routing and rewrite rules;
# Run the following command from the project root:
#
# php -S localhost:5000 -t path/to/wordpress path/to/router.php
#
# ^ hostname ^ root ^ server router
@bcole808
bcole808 / wp_embedly.php
Created May 7, 2015 21:37
WordPress Embedly Integration
<?php
/***************************
* Custom implementation of the Embedly service
****************************/
class Embedly_WordPress_Integration {
private $cache_ttl = WEEK_IN_SECONDS;
private $embedly_uri = 'http://api.embed.ly/1/oembed';
@bcole808
bcole808 / throttle.js
Created October 16, 2014 23:40
Throttle Javascript Functions
/**
* Debounce a function so that it will only fire once
*
* USAGE EXAMPLE:
* $scope.search = debounce(performSearch, 500); // Bind to ng-keyup
*
* http://davidwalsh.name/javascript-debounce-function
*
* @param func [Function] - The function to execute
@bcole808
bcole808 / wpms_fix_upload_paths.php
Created June 13, 2014 18:57
wp_upload_dir() multsite fix
<?php
/***************************************************
* Filter the results of wp_upload_dir()
*
* In WordPress Multisite systems that were installed prior to 3.5 the system uses the folder structure /wp-content/blogs.dir/XX to store uploaded files for each blog.
*
* The URL used to access these files follows the format /SITENAME/files/ but the function wp_upload_dir() does not match this format
***************************************************/
function wpms_fix_upload_paths($data) {
@bcole808
bcole808 / readme.md
Last active August 29, 2015 14:00
How to implement the WordPress Media Uploader

WP Media Uploader implementation

Starting in WordPress version 3.5, a new multimedia uploader is available to developers! Wohooo! This allows users to pick or upload an image file and then have that object data returned to your javascript for use any way you like.

In your functions.php file

Make sure to call wp_enqueue_media() from within the admin_enqueue_scripts action hook. This magically adds all dependencies to the page. Includes sugar, spice, and everything nice.

add_action('admin_enqueue_scripts', my_enqueue_scripts);
@bcole808
bcole808 / anchorScrolling.js
Last active August 29, 2015 13:57
Smooth scrolling to anchors using jQuery
$(document).ready(function() {
$('a[href^="#"]').on('click',function (e) {
e.preventDefault();
/***************
* CONFIGURATION
****************/
// How fast should the transiion be?