[Support request] Add/update Elements for specific user role

Home Forums Support [Support request] Add/update Elements for specific user role

Home Forums Support Add/update Elements for specific user role

Viewing 15 posts - 1 through 15 (of 38 total)
  • Author
    Posts
  • #680746
    Mitchell

    From what I can tell a user role has to have been granted the ‘manage_options’ capability to be able to get to Appearance > Elements and add/update/remove.

    I would like to give users with a specific role the ability to do this without enabling the ‘manage_options’ capability itself because then it opens up the ability to view/modify settings for a lot of other plug-ins and such that I do not want those users to have be access to get into.

    Is there a way to update/modify the capability assigned to grant access to the Elements module so I could grant access to this capability separately?

    Thanks!

    #681292
    Tom
    Lead Developer
    Lead Developer

    Hi there,

    You could try something like this:

    add_filter( 'register_post_type_args', function( $args, $post_type ) {
        if ( 'gp_elements' === $post_type ) {
            $args['capability_type'] = 'manage_options'; // Change to whatever.
        }
    
        return $args;
    }, 10, 2 );
    #988799
    Michael

    This worked for me:

    add_filter( 'register_post_type_args', function( $args, $post_type ) {
        if ( 'gp_elements' === $post_type ) {
            $args['capability_type'] = 'post';
        }
    
        return $args;
    }, 10, 2 );

    Now i need to show the menu on the left for editors.
    How can i do it?
    It’s odd that in GP editors don’t have access to this website content?
    Can we had it on the next version?

    Thank you

    #989290
    Tom
    Lead Developer
    Lead Developer

    Hi there,

    Just to confirm, do you mean the “Elements” link under the “Appearance” section?

    Let me know 🙂

    #989409
    Michael

    yes,

    Appearance > Elements

    my clients need to edit the content but usually i dont put them admin for security reasons 🙂

    #989899
    Tom
    Lead Developer
    Lead Developer

    Hmm, it seems add_submenu_page() doesn’t have a filter we can tap into, which is a shame.

    I’ve added a new filter to GPP 1.9: generate_elements_admin_menu_capability

    If you’d like to add this filter now, you can:

    1. Go to wp-content/plugins/gp-premium/elements/class-post-type.php
    2. Find the menu_item() function on line 252.
    3. Replace manage_options with apply_filters( 'generate_elements_admin_menu_capability', 'manage_options' )

    Now, we can do this:

    add_filter( 'generate_elements_admin_menu_capability', function() {
        return 'post';
    } );
    #993596
    Michael

    Hello,

    For me it doesn’t work.
    When i implement your code, the “Elements” disappears in the administrators UI but “Appearance” and “Appearance > Elements” doesn’t show up in the editors UI.

    Thank you

    #993931
    Tom
    Lead Developer
    Lead Developer

    Try this instead:

    add_filter( 'generate_elements_admin_menu_capability', function() {
        return 'edit_posts';
    } );
    #994520
    Michael

    hello again,

    The menu gets perfectly displayed now.
    But when i am an editor and i click on “Appearance > Elements”, i am blocked by the wordpress admin because i don’t have the correct permission apparently for edit.php?post_type=gp_elements.

    Thank you

    #994522
    Michael

    apparently we need to create the capabilities and map them…
    http://justintadlock.com/archives/2010/07/10/meta-capabilities-for-custom-post-types

    #995019
    Tom
    Lead Developer
    Lead Developer

    Is this being caused by this function?: https://generatepress.com/forums/topic/add-update-elements-for-specific-user-role/#post-988799

    Are you able to access as an admin?

    #997004
    Michael

    hello

    this is my code so far :

    functions.php:

    add_filter( 'generate_elements_admin_menu_capability', function() {
        return 'edit_posts';
    } );
    
    add_filter( 'register_post_type_args', function( $args, $post_type ) {
        if ( 'gp_elements' === $post_type ) {
            $args['capability_type'] = 'post';
        }
        return $args;
    }, 10, 2 );

    plugins/gp-premium/elements/class-post-type.php :

    /**
    * Create our admin menu item.
     *
    * @since 1.7
    */
    public function menu_item() {
        add_submenu_page(
    	'themes.php',
    	esc_html__( 'Elements', 'gp-premium' ),
    	esc_html__( 'Elements', 'gp-premium' ),
    	apply_filters( 'generate_elements_admin_menu_capability', 'manage_options' ),
    	'edit.php?post_type=gp_elements'
        );
    }

    goal 1 – show menu in UI => “generate_elements_admin_menu_capability” filter
    goal 2 – give permission => “register_post_type_args” filter

    in my tests, the two filters work separately but together not.

    thank you

    #997345
    Tom
    Lead Developer
    Lead Developer

    So the register_post_type_args filter works as long as the generate_elements_admin_menu_capability isn’t there?

    Looking at the Elements post type code, it should work for editors by default, so you shouldn’t need the register_post_type_args filter. Only the generate_elements_admin_menu_capability filter should be necessary.

    Let me know 🙂

    #1017299
    Michael

    Hello,

    I just tested on a brand new wordpress last version of theme and plugin and it doesn’t work for me. “register_post_type_args” give the permissions but doesn’t show the menu in admin UI.
    i think i will just pass because the wordpress documentation is not clear.

    thank you

    #1017807
    Tom
    Lead Developer
    Lead Developer

    Did that fresh install have the changes made to GP Premium I mentioned up above?: https://generatepress.com/forums/topic/add-update-elements-for-specific-user-role/#post-989899

Viewing 15 posts - 1 through 15 (of 38 total)
  • You must be logged in to reply to this topic.