Community Forums › Forums › Archived Forums › Design Tips and Tricks › Restrict access to custom post type on the front end.
- This topic has 3 replies, 2 voices, and was last updated 9 years, 5 months ago by Brad Dalton.
-
AuthorPosts
-
October 3, 2014 at 3:27 am #126644jourdainMember
Hi guys,
I have a little question that hopefully I can get some assistance with.
I have added a custom post type to my site using the following tutorial by Brad Dalton http://wpsites.net/web-design/add-custom-post-type/.
So my code looks exactly like his tutorial apart from the fact I have changed the name of the custom post type and also allowed categories in the taxonomies.
//* Create Custom Post Type add_action( 'init', 'add_custom_post_type' ); function add_custom_post_type() { register_post_type( 'members', array( 'labels' => array( 'name' => __( 'Members', 'wpsites' ), 'singular_name' => __( 'Member', 'wpsites' ), ), 'has_archive' => true, 'hierarchical' => true, 'menu_icon' => 'dashicons-admin-users', 'public' => true, 'rewrite' => array( 'slug' => 'members', 'with_front' => false ), 'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'trackbacks', 'custom-fields', 'revisions', 'page-attributes' ), 'taxonomies' => array( 'category' ), 'menu_position' => 2, )); }
Now what I would like to do is restrict access to this post type on the front end. I know this can be achieved with a content restriction plugin but I was wondering if there was a way to achieve this within the code. So I only want this post type to be seen by a "Group Leader" or "Admin" role.
If this cant be achieved in the code then please suggest the best plugin / solution to achieve what I am looking for.
I look forward to hearing your replies.
Jourdain
October 3, 2014 at 3:51 am #126647Brad DaltonParticipantDepends on what you want to restrict http://wpsites.net/wordpress-admin/restrict-access-to-custom-post-types-by-users-capability/
October 3, 2014 at 6:23 am #126659jourdainMemberOn second thoughts, I am going to be using genesis featured widget amplified to display the posts from the custom post type in a widget. Would it be easier to display the widget conditionally?
Basically I am creating a teacher support page but the content needs to be visible only when the user role is "Group Leader" or "admin". I can just remove the widget for anyone that is not a "Group Leader" or "admin", correct?
My code at the moment looks like
//* Register widget area for support page. genesis_register_sidebar( array( 'id' => 'new-widget', 'name' => __( 'Support Widget', 'domain' ), 'description' => __( 'Add Content Here', 'domain' ), ) ); add_action( 'genesis_entry_content', 'your_widget' ); function your_widget() { if ( is_page( 4825 ) and is_user_logged_in() && is_active_sidebar('new-widget') ) { genesis_widget_area( 'new-widget', array( 'before' => '<div class="new-widget widget-area">', 'after' => '</div>', ) ); } }
So my question is how to I display the widget conditionally based on user role? No doubt you have a tutorial already written for that. 🙂
Jourdain
October 3, 2014 at 6:39 am #126661Brad DaltonParticipantTry
if ( current_user_can('update_core') )
or
if ( is_admin() )
You will also need to display a login form or redirect to the login page
Does your code work?
Never seen and used before in a conditional statement
I have used AND or &&
-
AuthorPosts
- The forum ‘Design Tips and Tricks’ is closed to new topics and replies.