Skip to content

Instantly share code, notes, and snippets.

@benhuson
Last active August 29, 2015 14:04
Show Gist options
  • Save benhuson/9c419282d8e05fff6eeb to your computer and use it in GitHub Desktop.
Save benhuson/9c419282d8e05fff6eeb to your computer and use it in GitHub Desktop.
List Pages Shortcode Index - Create alphabetically indexes using the List Pages Shortcode plugin (http://wordpress.org/extend/plugins/list-pages-shortcode/)
<?php
/*
Plugin Name: List Pages Shortcode Index
Plugin URI: https://gist.github.com/benhuson/9c419282d8e05fff6eeb
Description: Add-on for the <a href="https://wordpress.org/plugins/list-pages-shortcode/">List Pages Shortcode</a> plugin.
Author: Ben Huson
Version: 0.1
Author URI: https://github.com/benhuson
*/
/**
* Allows you to add an 'index' attribute to the List Pages Shortcode plugin.
* https://wordpress.org/plugins/list-pages-shortcode/
* Example usage:
*
* [list-pages sort_order="ASC" sort_column="post_title" index="0123456789ABCDEF" title_li="A-F" /]
* [list-pages sort_order="ASC" sort_column="post_title" index="FGHIJKLM" title_li="F-M" /]
* [list-pages sort_order="ASC" sort_column="post_title" index="NOPQRST" title_li="N-T" /]
* [list-pages sort_order="ASC" sort_column="post_title" index="UVWXYZ" title_li="U-Z" /]
*/
class List_Pages_Shortcode_Index {
protected $index = '';
/**
* Setup Actions
*/
public function __construct() {
add_action( 'shortcode_list_pages_before', array( $this, 'shortcode_list_pages_before' ) );
add_action( 'shortcode_list_pages_after', array( $this, 'shortcode_list_pages_after' ) );
}
/**
* Filter the pages
*
* @param array $pages Pages.
* @param array $r List page args.
* @return array Filtered pages.
*/
public function shortcode_list_pages_get_pages( $pages, $r ) {
$filtered_pages = array();
// If no characters, return all pages
if ( empty( $this->index ) || 0 == $r['depth'] ) {
return $pages;
}
// Filter pages
foreach ( $pages as $page ) {
$character = strtolower( substr( $page->post_title, 0, 1 ) );
if ( strpos( $this->index, $character ) !== false ) {
$filtered_pages[] = $page;
}
}
return $filtered_pages;
}
/**
* Filter page args
*
* @param array $args List pages args.
* @return array Filtered args.
*/
public function shortcode_list_pages_attributes( $args ) {
// If index is set we can currently only handle one depth, not nested lists
if ( ! empty( $this->index ) ) {
$args['depth'] = 1;
}
return $args;
}
/**
* Store index characters and setup pages filter.
*
* @param array $atts Shortcode attributes.
*/
public function shortcode_list_pages_before( $atts ) {
// Store characters to index
$this->index = '';
if ( isset( $atts['index'] ) ) {
$this->index = strtolower( $atts['index'] );
}
add_filter( 'shortcode_list_pages_attributes', array( $this, 'shortcode_list_pages_attributes' ), 10, 1 );
add_filter( 'get_pages', array( $this, 'shortcode_list_pages_get_pages' ), 10, 2 );
}
/**
* Remove pages filter.
*
* @param array $atts Shortcode attributes.
*/
public function shortcode_list_pages_after( $atts ) {
remove_filter( 'shortcode_list_pages_attributes', array( $this, 'shortcode_list_pages_attributes' ), 10, 1 );
remove_filter( 'get_pages', array( $this, 'shortcode_list_pages_get_pages' ), 10, 2 );
}
}
global $list_pages_shortcode_index;
$list_pages_shortcode_index = new List_Pages_Shortcode_Index();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment