Skip to content

Instantly share code, notes, and snippets.

@fabienlege
Last active November 6, 2017 13:37
Show Gist options
  • Save fabienlege/9dd327e0dcafd7d27f2095abe82f58fa to your computer and use it in GitHub Desktop.
Save fabienlege/9dd327e0dcafd7d27f2095abe82f58fa to your computer and use it in GitHub Desktop.
add_filter( 'nav_menu_meta_box_object', 'show_private_pages_menu_selection' );
/**
* Add query argument for selecting pages to add to a menu
*/
function show_private_pages_menu_selection( $args ){
if( $args->name == 'page' ) {
$args->_default_query['post_status'] = array('publish','private');
}
return $args;
}
/**
* Ne pas afficher un lien de menu si l'utilisateur n'a pas le droit de le voir.
*/
function filter_private_pages_from_menu ($items, $args) {
foreach ($items as $ix => $obj) {
if (!is_user_logged_in () && 'private' == get_post_status ($obj->object_id)) {
unset ($items[$ix]);
}
}
return $items;
}
add_filter ('wp_nav_menu_objects', __NAMESPACE__.'\\filter_private_pages_from_menu', 10, 2);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment