Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save wpsmith/4203697 to your computer and use it in GitHub Desktop.
Save wpsmith/4203697 to your computer and use it in GitHub Desktop.
Private page filters for WordPress. Related to ticket #8592.
/**
* Add private/draft/future/pending pages to parent dropdown in page attributes metabox and Quick Edit
*
* @param array $dropdown_args
* @param object $post (Optional)
* @return array $dropdown_args
*/
function page_attributes_metabox_add_parents( $dropdown_args, $post = NULL ) {
$dropdown_args['post_status'] = array('publish', 'draft', 'pending', 'future', 'private');
return $dropdown_args;
}
add_filter( 'page_attributes_dropdown_pages_args', 'page_attributes_metabox_add_parents', 10, 2 );
add_filter( 'quick_edit_dropdown_pages_args', 'page_attributes_metabox_add_parents', 10);
/**
* Add (status) to titles in page parent dropdowns
*
* @param string $title
* @param object $page
* @return string $title
*/
function page_parent_status_filter( $title, $page ) {
$status = $page->post_status;
if ($status != __('publish'))
$title .= " ($status)";
return $title;
}
add_filter( 'list_pages', 'page_parent_status_filter', 10, 2);
/**
* Filter public page queries to include privately published ones.
* Filter pages metabox on menu admin screen to include all built-in statuses.
*
* @param object $query
* @return object $query
*/
function private_page_query_filter($query) {
if ( is_admin() ) {
$screen = get_current_screen();
if ( 'nav-menus' == $screen['base'] )
$query->set( 'post_status', 'publish,private,future,pending,draft' );
}
else {
$query->set( 'post_status', 'publish,private' );
}
return $query;
}
add_filter('pre_get_posts', 'private_page_query_filter');
/**
* Filter lists of pages to include privately published ones.
* This a duplicate of wp_list_pages() except we change post_status in the default args, and we return without echoing.
*
* @param string $output Original output of wp_list_pages(), which we will overwrite.
* @param array|string $args Parsed arguments passed from wp_list_pages().
* @return string HTML content.
*/
function wp_list_pages_with_private($output, $args) {
$defaults = array( 'post_status' => 'publish,private' ); // other defaults already parsed in wp_list_pages()
$r = wp_parse_args( $args, $defaults );
extract( $r, EXTR_SKIP );
$output = '';
$current_page = 0;
// sanitize, mostly to keep spaces out
$r['exclude'] = preg_replace('/[^0-9,]/', '', $r['exclude']);
// Allow plugins to filter an array of excluded pages (but don't put a nullstring into the array)
$exclude_array = ( $r['exclude'] ) ? explode(',', $r['exclude']) : array();
$r['exclude'] = implode( ',', apply_filters('wp_list_pages_excludes', $exclude_array) );
// Query pages.
$r['hierarchical'] = 0;
$pages = get_pages($r);
if ( !empty($pages) ) {
if ( $r['title_li'] )
$output .= '<li class="pagenav">' . $r['title_li'] . '<ul>';
global $wp_query;
if ( is_page() || is_attachment() || $wp_query->is_posts_page )
$current_page = $wp_query->get_queried_object_id();
$output .= walk_page_tree($pages, $r['depth'], $current_page, $r);
if ( $r['title_li'] )
$output .= '</ul></li>';
}
return $output;
}
add_filter('wp_list_pages', 'wp_list_pages_with_private', 1, 2);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment