Home > Uncategorized > Create a menu for private pages and posts in WordPress | ActiveCodeline

Create a menu for private pages and posts in WordPress | ActiveCodeline

September 14th, 2010

A feature to implement. We want to have member-only items in the top menu line. I think this is a fantastic idea. We’ll do this later..

—————————–

Create a menu for private pages and posts in WordPress

Posted by branko in General | 14 Comments

Recently I was working on a project that required a members menu on sidebar. Idea was to mark some pages and posts in WordPress as a private ones. All those marked as private are not to be accessible to all others, unregistered users, or even some registered that do not belong to some assigned role. WordPress default behavior for posts and pages is not to show up on menu if you are not registered user, which is good. However, another WordPress default behavior is to show only private posts on sidebar menu while logged in. Meaning that pages marked as private do not show up on the menu when logged in. This is not so good. I needed a solution that will list private pages on my menu as well. After few minutes of unsuccessful Google search, i decided to write my own solution. It really wasn’t that difficult. It all came down to two existing WordPress functions, current_user_can(‘some_role’) and $wpdb->get_results($some_query).

/*show pages and post mixed on menu
$menu_type = post ->show posts only
$menu_type = page ->show pages only

*/

if(current_user_can($user_role)) {

global $wpdb;
$html = null;

if($menu_type != 'all') {
$menu_query = "post_status='private' AND post_type='". $menu_type ."'";
}

else if($menu_type == 'all') {
$menu_query = "post_status='private'";
}

$query = "SELECT * FROM $wpdb->posts WHERE ". $menu_query;
$pages = $wpdb->get_results($query);

$html .= '

';

return $html;

}

else {

$html = '

We are sorry, but only '. $user_role .' role user can access this area

';
# return $html;
return false;
}
}
?>

via Create a menu for private pages and posts in WordPress | ActiveCodeline.

Categories: Uncategorized Tags: