Need help in making capabilities

This topic contains 1 reply, has 2 voices, and was last updated by  Paolo 7 years, 6 months ago.

We have moved to a support ticketing system and our forums are now closed.

Open Support Ticket
  • Author
    Posts
  • #295032

    Aashish Gangwani
    Free User

    Hi,

    I am new to geodirectory. I just want one thing, letting subscribers to add, edit and delete places.

    I have done the following:
    1. Added $capabilites array to $place-default which I suppose is the argument to register_post_type
    2. Changed capability_type to ‘place’ in the same $place_default
    3. Added filter to map_meta_cap as a function “places_map_meta_cap”

    
    
    
    
    Here is the code demonstrating the same.
    //capabilities array
    $capabilities = array(
        'publish_posts' => 'publish_places',
        'edit_posts' => 'edit_places',
        'edit_others_posts' => 'edit_others_places',
        'delete_posts' => 'delete_places',
        'delete_others_posts' => 'delete_others_places',
        'read_private_posts' => 'read_private_places',
        'edit_post' => 'edit_place',
        'delete_post' => 'delete_place',
        'read_post' => 'read_place'
    );
    
    //I think $place_default is the argument to register_post_type() so I added $capabilities array to it and changed capability_type to 'place'
    			
            $place_default = array(
                'labels' => $labels,
                'can_export' => true,
                'capability_type' => 'post',
                'description' => 'Place post type.',
                'has_archive' => $listing_slug,
                'hierarchical' => false,
                'map_meta_cap' => true,
                'menu_icon' => $menu_icon,
                'public' => true,
    			'capability_type' => 'place',
                'capabilities' => $capabilities,
                'query_var' => true,
                'rewrite' => array('slug' => $listing_slug , 'with_front' => false, 'hierarchical' => true, 'feeds' => true),
                'supports' => array('title', 'editor', 'author', 'thumbnail', 'excerpt', 'custom-fields', 'comments', /*'revisions', 'post-formats'*/),
                'taxonomies' => array('gd_placecategory', 'gd_place_tags'));
    

    After adding $capabilities array and changing capability type I added a filter to map_meta_cap by adding a function called “places_map_meta_cap”

    Here is the code of the same:

    
    
    
    /* MAP META CAPABILITIES */
    
    add_filter( 'map_meta_cap', 'places_map_meta_cap', 10, 4 );
    
    function places_map_meta_cap( $caps, $cap, $user_id, $args )
    {
    
        if ( 'edit_place' == $cap || 'delete_place' == $cap || 'read_place' == $cap ) {
            $post = get_post( $args[0] );
            $post_type = get_post_type_object( $post->post_type );
            $caps = array();
        }
    
        if ( 'edit_place' == $cap ) {
            if ( $user_id == $post->post_author )
                $caps[] = $post_type->cap->edit_posts;
            else
                $caps[] = $post_type->cap->edit_others_posts;
        }
    
        elseif ( 'delete_place' == $cap ) {
            if ( $user_id == $post->post_author )
                $caps[] = $post_type->cap->delete_posts;
            else
                $caps[] = $post_type->cap->delete_others_posts;
        }
    
        elseif ( 'read_place' == $cap ) {
            if ( 'private' != $post->post_status )
                $caps[] = 'read';
            elseif ( $user_id == $post->post_author )
                $caps[] = 'read';
            else
                $caps[] = $post_type->cap->read_private_posts;
        }
    
        return $caps;
    }
    

    I had followed this post: http://stackoverflow.com/questions/8198038/wordpress-capabilities-for-custom-post-types

    to do this and I think the procedure and execution is perfect but when I checked in user-role-editor plugin none capabilities were displayed I wonder why. Please please help me in this.

    #295231

    Paolo
    Site Admin
    Post count: 31206

    Hi,

    GeoDirectory allows subscribers to add/edit/delete listings out of the box.

    No code is needed to do that, unless I misunderstood your question, in that case let me know exactly what you are trying to do.

    Thanks

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

We have moved to a support ticketing system and our forums are now closed.

Open Support Ticket