Instantly share code, notes, and snippets.
Last active
December 4, 2023 06:44
-
Star
0
(0)
You must be signed in to star a gist -
Fork
1
(1)
You must be signed in to fork a gist
-
Save jwenerd/d3ec5e14895264545806 to your computer and use it in GitHub Desktop.
Page navigation shortcode for WordPress
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @package Page Navigation Shortcode | |
* @version 0.1 | |
*/ | |
/* | |
Plugin Name: Page Navigation Shortcode | |
Description: This plugin automatically adds links to the previous and next pages at the bottom of page content. User's may disable for select pages as well on the page edit screen. | |
Author: TLT Studio | |
Version: 0.1 | |
Author URI: http://studio.tlt.psu.edu/ | |
*/ | |
if(!class_exists('Page_Navigation')){ | |
class Page_Navigation { | |
const CACHE_GROUP = 'page-navigation'; | |
function __construct(){ | |
add_filter('the_content', array(&$this,'add_shortcode')); | |
add_shortcode('page_navigation', array(&$this,'do_shortcode')); | |
if(is_admin()){ | |
add_action('save_post_page', array(&$this,'purge_cache')); | |
add_action('save_post_page', array(&$this,'save_meta')); | |
add_action('add_meta_boxes', array(&$this,'add_meta_box')); | |
} | |
} | |
function add_meta_box(){ | |
add_meta_box( | |
'page-navigation-meta-box', // id, used as the html id att | |
__( 'Page Navigation' ), // meta box title, like "Page Attributes" | |
array(&$this,'meta_box'), // callback function, spits out the content | |
'page', // post type or page. We'll add this to pages only | |
'side', // context (where on the screen | |
'low' // priority, where should this go in the context? | |
); | |
} | |
function meta_box($post){ | |
wp_nonce_field( 'page_navigation_meta_box', 'page_navigation_meta_box_nonce' ); | |
$value = get_post_meta( $post->ID, '_page_navigation_append', true ); | |
if($value == ''){ | |
$value = 1; | |
} | |
echo '<input type="checkbox" id="page-navigation-append" name="page_navigation_append" value="1" '. checked($value, 1,false) .' />'; | |
echo '<label for="page-navigation-append">'; | |
_e( 'Show Page Navigation'); | |
echo '</label> '; | |
echo '<p>If checked links to the next and previous pages will be shown at the bottom of the page content.</p>'; | |
} | |
function save_meta($post_id ){ | |
if ( ! isset( $_POST['page_navigation_meta_box_nonce'] ) ) { | |
return; | |
} | |
if ( ! wp_verify_nonce( $_POST['page_navigation_meta_box_nonce'], 'page_navigation_meta_box' ) ) { | |
return; | |
} | |
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { | |
return; | |
} | |
if ( ! current_user_can( 'edit_page', $post_id ) ) { | |
return; | |
} | |
$value = (isset($_POST['page_navigation_append'])) ? '1' : '0'; | |
update_post_meta($post_id, '_page_navigation_append', $value); | |
} | |
function add_shortcode($content){ | |
global $post; | |
// only do this for pages | |
if($post->post_type != 'page') | |
return $content; | |
// don't add if the shortcode is already present | |
if(strpos($content, '[page_navigation') !== false) | |
return $content; | |
$value = get_post_meta($post->ID, '_page_navigation_append',true); | |
if($value == '0') | |
return $content; | |
// value is not present or set already - so go ahead and add the shortcode | |
return "{$content}\n\n[page_navigation]"; | |
} | |
function do_shortcode($args){ | |
global $post; | |
if($post->post_type != 'page') | |
return; | |
// not used yet - but hope to support | |
// querying the post tree via the an actual menu | |
$args = shortcode_atts(array( | |
'navigation' => 'pages' | |
), $args); | |
// we have a namespace key for the cache so we can invalidate it be incrementing the prefix when pages are updated | |
$prefix = wp_cache_get(self::CACHE_GROUP.'_key', self::CACHE_GROUP); | |
if ($prefix === false ){ | |
$prefix = 1; | |
wp_cache_set(self::CACHE_GROUP.'_key', $prefix , self::CACHE_GROUP); | |
} | |
$cache_key = "p{$prefix}-{$post->ID}"; | |
$output = wp_cache_get($cache_key, self::CACHE_GROUP); | |
if($output === false){ | |
$next = $this->get_next_page($args['navigation']); | |
$prev = $this->get_previous_page($args['navigation']); | |
$output = "{$next}{$prev}"; | |
wp_cache_set($cache_key, $output, self::CACHE_GROUP); | |
} | |
return $output; | |
} | |
function get_next_page($navigation = 'pages'){ | |
global $post; | |
$get_pages = get_pages('child_of='.$post->post_parent.'&sort_column=menu_order&sort_order=asc'); | |
$pages_count = count($get_pages); | |
// look for child page first | |
for($p=0; $p < $pages_count; $p++) { | |
// get the array key for our entry | |
if ($post->ID == $get_pages[$p]->ID) break; | |
} | |
// assign our next key | |
$next_key = $p+1; | |
$output = false; | |
// if there isn't a value assigned for the previous key, go all the way to the end | |
if (isset($get_pages[$next_key])) { | |
$anchor_name = $get_pages[$next_key]->post_title; | |
$output = '<a class="page-navigation next" style="text-align:right; float:right; display:block; width:49%;" href="'.get_permalink($get_pages[$next_key]->ID).'" title="'.esc_attr($anchor_name).'">Next Page: '.esc_html($anchor_name).'</a>'; | |
} else { | |
$get_parent_pages = get_pages('sort_column=menu_order&sort_order=asc'); | |
$parent_page_count = count($get_parent_pages); | |
for($pp=0; $pp < $parent_page_count; $pp++) { | |
// get the array key for our entry | |
if ($post->ID == $get_parent_pages[$pp]->ID) break; | |
} | |
// assign our next key | |
$parent_page_count = $pp+1; | |
if (isset($get_parent_pages[$parent_page_count])) { | |
$anchor_name = $get_parent_pages[$parent_page_count]->post_title; | |
$output = '<a class="page-navigation next" style="text-align:right; float:right; display:block; width:49%;" href="'.get_permalink($get_parent_pages[$parent_page_count]->ID).'" title="'.esc_attr( $anchor_name ) .'">Next Page: '.esc_html($anchor_name).'</a>'; | |
} | |
} | |
return $output; | |
} | |
function get_previous_page($navigation = 'pages'){ | |
global $post; | |
$get_pages = get_pages('child_of='.$post->post_parent.'&sort_column=menu_order&sort_order=asc'); | |
$pages_count = count($get_pages); | |
// look for child page first | |
for($p=0; $p < $pages_count; $p++) { | |
// get the array key for our entry | |
if ($post->ID == $get_pages[$p]->ID) break; | |
} | |
// assign our next key | |
$prev_key = $p-1; | |
$output = false; | |
// if there isn't a value assigned for the previous key, go all the way to the end | |
if (isset($get_pages[$prev_key])) { | |
$anchor_name = $get_pages[$prev_key]->post_title; | |
$output = '<a style="float:left; display:block; width:49%;" class="page-navigation prev" href="'.get_permalink($get_pages[$prev_key]->ID).'" title="'.esc_attr($anchor_name).'">Previous Page: '.esc_html($anchor_name).'</a>'; | |
} else if ($post->post_parent != 0) { | |
$anchor_name = get_the_title($post->post_parent); | |
$output = '<a style="float:left; display:block; width:49%;" class="page-navigation prev" href="'.get_permalink($post->post_parent).'" title="'.esc_attr( $anchor_name ).'">Previous Page: '.esc_html($anchor_name).'</a>'; | |
} | |
return $output; | |
} | |
function purge_cache(){ | |
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { | |
return; | |
} | |
// increment our prefix value when a page is updated, this will invalidate | |
// previous set cache items, let memcached garbage collection take care of the | |
// removing the bad values from memory | |
wp_cache_increment(self::CACHE_GROUP.'_key', 1, self::CACHE_GROUP); | |
} | |
} | |
$page_navigation = new Page_Navigation(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment