• Resolved pkarjala

    (@pkarjala)


    I’m a bit new at creating Custom Post Types, but I think I have the basics down. For a theme that I’m writing for a client I’m creating a new Post Type called “Performance Task” that a teacher will be able to write and post about a lesson plan they’ve created.

    However, when I activate the theme, the Administrator account can only access the Categories and Taxonomies for the Post Type; it cannot access any other abilities such as new post, view posts, etc.

    I’ve dug through the documentation and haven’t been able to find anything on registering post types that coincides with needing to grant the Administrator role additional access. However, I could also just be doing it completely wrong.

    A gist of the php to create the custom post type is at https://gist.github.com/pkarjala/d827a96d029294ed4485

    My choice of doing this in a theme vs. a plugin aside, I’d like to learn how to provide the administrator role access to all of the normal functionality that the new post type has: create, publish, delete, etc. I’ll eventually be adding a few other custom roles that can interact with this post type, but I think I can get that working correctly. Do I need to re-create the administrator role? Or can I simply add the new capability required to administrator in the theme?

    Thank you for your assistance.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The Admin does not inherit those capabilities, e.g.

    $performance_task_capabilities = array(
    		'edit_post'           => 'edit_performance_task',
    		'read_post'           => 'read_performance_task',
    		'delete_post'         => 'delete_performance_task',
    		'edit_posts'          => 'edit_performance_task',
    		'edit_others_posts'   => 'edit_others_performance_tasks',
    		'publish_posts'       => 'publish_performance_task',
    		'read_private_posts'  => 'read_private_performance_task',
    	);

    …just because they are an admin, you have to actually add the capabilities to the admin and any other roles you want to be able to access them. You can can use the theme activation hook to add those capabilities to all the roles, and you would include the Administrator role.

    Of course, if you wanted, you could use the de-activation hook to remove the capabilities.

    Or, you could add them on a per role basis; so when the user logs in, if they meet the role requirement (including admin’s) you would ->add_cap() to that user. This might be less DB intensive for a large site.

    If you’re looking for a plugin solution, check out: https://wordpress.org/plugins/custom-post-type-ui/

    You can go through all the users using:

    http://codex.wordpress.org/get_users

    You can check if they have the capability by using:

    https://codex.wordpress.org/Function_Reference/current_user_can

    You can get just the logged in user using:

    https://codex.wordpress.org/Function_Reference/wp_get_current_user

    And add the capability using:

    http://codex.wordpress.org/add_cap

    Hope this helps.

    Thread Starter pkarjala

    (@pkarjala)

    Thanks Aubrey; this is exactly the info I needed to confirm, along with the method to add capability instead of re-creating the user from scratch using add_cap.

    The primary way I’ll be handling it is adding the capabilities to roles in the theme activation hook as suggested, and then using the theme de-activation hook to return users to different roles if the theme is de-activated.

    I’m trying to avoid plugins at this time in order to really learn WP Core better, as I intend to write some plugins later on as I get more familiar with WordPress. I’ll keep the plugin solution in mind in case we run into a time constraint or other problems.

    Thank you again for your help.

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Custom Post Type, Admin can't Create’ is closed to new replies.