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 / redirect-wp-post.php
Last active April 12, 2023 09:37
Redirects wordpress posts to new url: site.com/blog/post-name
<?php
/**
* Add new rewrite rule
*/
function create_new_url_querystring() {
add_rewrite_rule(
'blog/([^/]*)$',
'index.php?name=$matches[1]',
'top'
);
@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 / functions.php
Last active January 17, 2023 20:30
Filter WordPress posts by custom taxonomy term with AJAX
<?php
/**
* AJAC filter posts by taxonomy term
*/
function vb_filter_posts() {
if( !isset( $_POST['nonce'] ) || !wp_verify_nonce( $_POST['nonce'], 'bobz' ) )
die('Permission denied');
/**
@Bobz-zg
Bobz-zg / main.js
Last active May 15, 2022 03:52
Filter WordPress posts by custom taxonomy term with AJAX - Javscript
(function($) {
$doc = $(document);
$doc.ready( function() {
/**
* Retrieve posts
*/
function get_posts($params) {
@Bobz-zg
Bobz-zg / shortcode.php
Last active September 7, 2022 11:17
Filter WordPress posts by custom taxonomy term with AJAX
<?php
/**
* Shortocde for displaying terms filter and results on page
*/
function vb_filter_posts_sc($atts) {
$a = shortcode_atts( array(
'tax' => 'post_tag', // Taxonomy
'terms' => false, // Get specific taxonomy terms only
'active' => false, // Set active term by ID
@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 / get-posts-2.js
Created August 25, 2016 10:06
Filter WordPress posts by custom taxonomy term with AJAX
function get_posts($params) {
$container = $('#container-async');
$content = $container.find('.content');
$status = $container.find('.status');
$status.text('Loading posts ...');
$.ajax({
url: bobz.ajax_url,
@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,