Skip to content

Instantly share code, notes, and snippets.

View Bobz-zg's full-sized avatar
👨‍💻
working remotely

Vlado Bosnjak Bobz-zg

👨‍💻
working remotely
View GitHub Profile
@Bobz-zg
Bobz-zg / wp-ajax-filter-posts-by-tag.js
Created August 12, 2016 16:03
Filter WP posts by tag - JS
jQuery(document).ready(function($) {
$('.tax-filter').click( function(event) {
// Prevent default action - opening tag page
if (event.preventDefault) {
event.preventDefault();
} else {
event.returnValue = false;
}
@Bobz-zg
Bobz-zg / wp-ajax-filter-posts-by-tag.php
Created August 12, 2016 16:06
Filter WP posts by tag - PHP
<?php
/**
* Enqueue JS file
* Be sure to update path to js
*
* @link https://gist.github.com/Bobz-zg/16578c3c173abfe2b22daffd67f6f1e0
*
*/
function ajax_filter_posts_scripts() {
@Bobz-zg
Bobz-zg / enqueue.php
Created August 24, 2016 18:46
Filter WordPress posts by custom taxonomy term with AJAX
<?php
function assets() {
wp_enqueue_script('tuts/js', 'scripts/tuts.js', ['jquery'], null, true);
wp_localize_script( 'tuts/js', 'bobz', array(
'nonce' => wp_create_nonce( 'bobz' ),
'ajax_url' => admin_url( 'admin-ajax.php' )
));
}
@Bobz-zg
Bobz-zg / get-posts-1.js
Created August 25, 2016 09:55
Filter WordPress posts by custom taxonomy term with AJAX
$('#container-async').on('click', 'a[data-filter], .pagination a', function(event) {
if(event.preventDefault) { event.preventDefault(); }
$this = $(this);
if ($this.data('filter')) {
/**
* Click on tag cloud
*/
$this.closest('ul').find('.active').removeClass('active');
@Bobz-zg
Bobz-zg / ajax-pagination-1.php
Created August 25, 2016 10:11
Filter WordPress posts by custom taxonomy term with AJAX
<?php
function vb_ajax_pager( $query = null, $paged = 1 ) {
if (!$query)
return;
$paginate = paginate_links([
'base' => '%_%',
'type' => 'array',
'total' => $query->max_num_pages,
(function($) {
function triggerError($err, $status) {
$msg = false;
$errors = {
'e100' : "Something went wrong, please try again later",
'e101' : "Please enter you'r username and password",
'e102' : "Please enter you'r email",
'e103' : "Please populate all fields marked with *",
@Bobz-zg
Bobz-zg / toggler.css
Created September 9, 2016 16:00
Simple jquery toggle function Raw
.toggler {
overflow: hidden;
max-height: 0;
transition: max-height .3s ease-in;
}
.toggler.active {
max-height: 2000px;
transition: max-height .3s ease-out;
}
@Bobz-zg
Bobz-zg / toggle.html
Created September 9, 2016 16:28
Simple reusable toggle with jquery
<a href="#toggleMe" data-toggle="toggleMe">Toggle div</a>
<div id="toggleMe" class="toggler">
Content goes here ...
</div>
@Bobz-zg
Bobz-zg / toggler.jquery.js
Last active September 9, 2016 16:37
Simple jquery toggle function
$.fn.toggler = function() {
return this.each( function() {
$(this).click(function(event) {
// Prevent click event on a tag
if ($(this).is('a')) {
if(event.preventDefault) { event.preventDefault(); }
}
@Bobz-zg
Bobz-zg / ajax-get-posts.php
Last active September 29, 2016 09:24
Filter WordPress posts by custom taxonomy term with AJAX
<?php
function vb_filter_posts() {
if( !isset( $_POST['nonce'] ) || !wp_verify_nonce( $_POST['nonce'], 'bobz' ) )
die('Permission denied');
/**
* Default response
*/
$response = [