Skip to content

Instantly share code, notes, and snippets.

View jlittlejohn's full-sized avatar

Josh Littlejohn jlittlejohn

View GitHub Profile
@jlittlejohn
jlittlejohn / gist:9a60294afc58f48cea80
Created September 10, 2014 16:43
JS: Create A-Z Filtering of a List
$("#glossary > .term").each(function() {
$(this).addClass("glossary-" + $(this).children("h3").text().charAt(0));
});
$('#glossary > #no-results').hide();
var resetDirectory = function () {
$("#glossary > .term").show();
};
@jlittlejohn
jlittlejohn / gist:43ce399a2b2dc0b5351a
Last active August 29, 2015 14:06
WP: Ajax Call to Custom Post Type
// Add to head tag in template
<script>
var ajaxurl = "<?php echo admin_url('admin-ajax.php'); ?>";
</script>
// Ajax Function for Returning Custom Post Type of Glossary
function test_ajax() {
header( "Content-Type: application/json");
@jlittlejohn
jlittlejohn / gist:9ea086554d5a8273aa75
Created September 10, 2014 16:31
WP: Build Custom Taxonomy Navigation
function build_category_navigation( $cat = null ){
$args = array(
'hierarchical' => 1,
'pad_counts' => 1,
'taxonomy' => 'categories',
'order_by' => 'id',
'order' => 'asc'
);
@jlittlejohn
jlittlejohn / gist:bb4a4512fe746b59f79a
Created September 10, 2014 16:29
WP: Create Shortcode
// Example
function shortcode_name( $atts, $content = null ){
$a = shortcode_atts( array(
'example' => ''
), $atts );
return '<a id="example" data-example="' . $a['example'] . '">' . $content . '</a>';
}
add_shortcode( 'name', 'shortcode_name' );
@jlittlejohn
jlittlejohn / gist:6204b26bd8e526aa0172
Last active May 1, 2019 15:58
JS: Check to see if a property exists on a Method
// Check if this.property exists, if so use it, if not create it and add value to the array
var object = {
method: function (value) {
this.property = this.property || [];
this.property.push(value);
}
};
@jlittlejohn
jlittlejohn / gist:f543e19d28af5ed0e589
Last active May 1, 2019 15:58
JS: Angular Factory to Get WP Posts
// Credit: Montague Monro
// https://www.youtube.com/watch?v=3WiyGmdOjbc
app.factory('Posts', ['$http', function ($http) {
return {
getPosts: function () {
var response = $http({
url: ajaxurl,
method: "GET",
params: {action: 'test_ajax'}
@jlittlejohn
jlittlejohn / gist:b6695971d706732a1165
Created August 26, 2014 14:37
WP: Enable Ajax in WordPress
<?php
// Put JavaScript into Template
function enable_ajax_functionality() {
wp_localize_script( 'ajaxify', 'ajaxify_function', array('ajaxurl' => admin_url('admin-ajax.php')) );
}
add_action('template_redirect', 'enable_ajax_functionality');
// Add two actions to wp_ajax, where we get posts array back as json
function test_ajax() {
@jlittlejohn
jlittlejohn / gist:92e5513bf40a2a002ae7
Created August 25, 2014 17:30
JS: Determine Scroll Direction
function init_scroll(event, delta) {
var deltaOfInterest = delta,
timeNow = new Date().getTime(),
quietPeriod = 500;
// Cancel scroll if currently animating or within quiet period
if(timeNow - lastAnimation < quietPeriod + settings.animationTime) {
event.preventDefault();
return;
}
@jlittlejohn
jlittlejohn / gist:3099f32a589021075c89
Last active May 1, 2019 15:57
JS: Basic jQuery Plugin Boilerplate
!function($) {
var defaults = {
sectionContainer: "section",
};
$.fn.onepage_scroll = function(options) {
var settings = $.extend({}, defaults, options);
}
@jlittlejohn
jlittlejohn / gist:3cac0daf7db0207373fb
Created August 13, 2014 18:19
JS: Change Data Attribute of Element on Click
$(".btn").bind("click", function () {
$(this).attr('data-state', 'pressed');
});